.NET MAUI:MVVM如何使用Shell Navigation从其他页面获得返回

发布于 2025-02-11 20:04:05 字数 842 浏览 2 评论 0 原文

我已经使用shell gotoasync导航设置了一个主页 - 详细信息页面导航

[RelayCommand] 
public async void SelectionChanged() //Friend friend
{
    if (SelectedItem == null) return;

    Friend f = SelectedItem;

    Console.WriteLine($"Selection made {f.FName} {f.LName}");

    //navigate
    var navigationParameter = new Dictionary<string, object>
    {
        { "Friend", f }
    };
    await Shell.Current.GoToAsync(nameof(DetailPage), true, navigationParameter);

    //remove selection highlight
    SelectedItem = null;
}

。但是,我对如何从主页中的细节页面中捕获返回,因为我需要刷新我的CollectionView和基础SQLITE数据存储。

我遵循了 https://www.youtube.com/watch?v=v=? PBH5SXVSWXW 大部分时间。

有什么想法吗?

非常感谢,G

I have set up a mainpage - detail page navigation using the Shell GoToAsync navigation

[RelayCommand] 
public async void SelectionChanged() //Friend friend
{
    if (SelectedItem == null) return;

    Friend f = SelectedItem;

    Console.WriteLine(
quot;Selection made {f.FName} {f.LName}");

    //navigate
    var navigationParameter = new Dictionary<string, object>
    {
        { "Friend", f }
    };
    await Shell.Current.GoToAsync(nameof(DetailPage), true, navigationParameter);

    //remove selection highlight
    SelectedItem = null;
}

This works. However, I am at a loss as to how to capture the return from the detailpage in my mainpage as I need to do a refresh of my CollectionView and underlying sqlite datastore.

I have followed the Gerald Versluis video at https://www.youtube.com/watch?v=pBh5SXVSwXw for the most part.

Any ideas?

Many thanks, G

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小矜持 2025-02-18 20:04:05

事实证明,这并不像看起来那样难。 @ToolMakerSteve指出了一个解决方案,但当时我不明白。解决方案是简单地使用消息中心方法。文档 https:// https://learlearn.microsoft.com/en-en-us/en-en-en-us/ dotnet/maui/fordamentals/Mendvingcenter 在用法上不太清楚,但是在

rel =“ nofollow noreferrer”> https://codemilltech.com/messing-with-xamarin-forms-messaging-center/ 我在详细信息页面ViewModel和返回主页的按钮中 (不是我尚未捕获的后按钮)。 (使用MVVM Helpers版本8.0.0.0 Preview 4(不是7,但可以改编),

[RelayCommand]
public async void ReturnMainPage()
{
  MessagingCenter.Send(new MessagingMarker(), "IDidSomething");
}

并且在构造函数中的MainPageViewModel中

public MainPageViewModel()
{
  ...
  MessagingCenter.Subscribe<MessagingMarker>(this, "IDidSomething", (s) => {
    //do something in response to the detail page changing something
  });
  ...
}

,希望将来对某人有帮助。
G.

顺便说一句,有人可以告诉我“这”是指的是什么?页面?根据代码的位置,在这里还可以将什么视为“此”可能是不合适的?

PS您也可以使用此对象传递对象,请参阅我的问题。net maui:如何从任何内容页面(mvvm)读取/写入(get/set)

As it turns out this was not as hard as it might have seemed. @ToolmakerSteve pointed out a solution, but I didn't understand it at the time. The solution is to simply use the MessagingCenter approach. The documents https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/messagingcenter aren't very clear on usage, but adapting the note in https://codemilltech.com/messing-with-xamarin-forms-messaging-center/ I did the following

In my detail page ViewModel and the button for returning to the main page (not the Back button which I haven't captured yet). (Using MVVM helpers version 8.0.0.0 Preview 4 (not 7, but could be adapted),

[RelayCommand]
public async void ReturnMainPage()
{
  MessagingCenter.Send(new MessagingMarker(), "IDidSomething");
}

and in the MainPageViewModel in the constructor

public MainPageViewModel()
{
  ...
  MessagingCenter.Subscribe<MessagingMarker>(this, "IDidSomething", (s) => {
    //do something in response to the detail page changing something
  });
  ...
}

Hope this helps someone in the future.
G.

btw can someone tell me what the "this" is referring to? The page? What else could be put here as "this" might not be appropriate depending on the location of the code?

PS You can pass objects using this as well, see my question on .Net Maui: How to read/write (get/set) a global object from any content page (MVVM)

月光色 2025-02-18 20:04:05

,但是当我单击一些时,我需要提出其他一些活动
按钮并导航。

如果您想在返回时做某事,则可以使用 Backbuttonbehavior 实现这一目标。

而且,如果您希望在单击某些按钮并返回时提出其他事件,则可以尝试以下代码:

 await Shell.Current.GoToAsync("..");

并可以将“ ..”向后导航与路由相结合:

 await Shell.Current.GoToAsync("../route");

有关更多信息,您可以检查:< a href =“ https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/shell/navigation#backwards-navigation” rel =“ nofollow noreferrer”>倒退导航。

but I'm needing some other event to be raised when I click on some
button and navigate back.

If you want to do something while navigating back, you can use BackButtonBehavior to achieve this.

And if you want some other event to be raised while clicking on some button and navigating back, you can try the following code :

 await Shell.Current.GoToAsync("..");

And backwards navigation with ".." can also be combined with a route:

 await Shell.Current.GoToAsync("../route");

For more,you can check: Backwards navigation .

吃颗糖壮壮胆 2025-02-18 20:04:05

MendagingCenter 已在.NET 7中弃用,并在communityToolkit.mvvm nuget软件包中替换为 。有关更多信息,请参见 docs

在您的主页上:

public class MessageModel() {
    public string Message {get;set;}
}

public MasterPage() {

   ...

   WeakReferenceMessenger.Default.Register<MessageModel>(this, (r, m) =>
   {
     // do something, reload view 
   });
}

在您的详细信息页面上回去:

MessageModel messagemodel = new() {
    Message = "IDidSomething"
};
WeakReferenceMessenger.Default.Send(messagemodel);

MessagingCenter has been deprecated in .NET 7 and replaced with WeakReferenceMessenger in the CommunityToolkit.Mvvm NuGet package. For more information, see docs

At your master page:

public class MessageModel() {
    public string Message {get;set;}
}

public MasterPage() {

   ...

   WeakReferenceMessenger.Default.Register<MessageModel>(this, (r, m) =>
   {
     // do something, reload view 
   });
}

and at your detail page when you go back:

MessageModel messagemodel = new() {
    Message = "IDidSomething"
};
WeakReferenceMessenger.Default.Send(messagemodel);
忆梦 2025-02-18 20:04:05

您可以使用外壳来捕获返回的动作。在本文档末尾解释:

您可以使用Shell Backbuttonbehavior并创建命令传递参数,就像导航到详细信息页面相同的方式。传递CollectionView以保持更新。

You can catch the action of returning, using the shell. Explained at the end of this documentation:
https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/shell/navigation

You can catch the return action, by using the shell BackButtonBehavior and creating a command passing parameters, the same way you did to navigate to detail page. Passing the collectionview to keep it updated.

弃爱 2025-02-18 20:04:05

我做到了Isidoros Moulas写的。

但是,要使我对我有用,请将主页对象(源页)发送到详细信息页面,然后返回值,我完成了以下操作:

中使用模型导航。

注意:我在我的主页

声明 作为我们要保存返回值

public string value {get; set;}

作为参数的属性,当我更改这样的页面时已经使用模型创建的实例发送:

await Navigation.PushModalAsync(new DetailPage(this));

详细页面,

我声明了类型MasterPage的属性

private MasterPage master_page;

,在构造函数中,dectover deck deck deck deck of the mainpage对象为参数。

 public ReaderCodesCamara(MasterPage masterPage)       
{
    InitializeComponent(); 
    master_page = masterPage;
}

为了将价值返回到主页:

master_page .value = "IDidSomething"; 
await App.Current.MainPage.Navigation.PopModalAsync();
WeakReferenceMessenger.Default.Send(master_page);

问候

I did it as Isidoros Moulas wrote.

But for it to work for me to, send the MainPage object (the source page) to the DetailPage and then return the value, I did the following:

Note: I use model navigation.

In my MainPage

Declare as property where we are going to save the returned value

public string value {get; set;}

Send as a parameter the instance already created with the model when I change the page like this:

await Navigation.PushModalAsync(new DetailPage(this));

In DetailPage,

I declared a property of type MasterPage

private MasterPage master_page;

and in constructor declated the MainPage object as a parameter.

 public ReaderCodesCamara(MasterPage masterPage)       
{
    InitializeComponent(); 
    master_page = masterPage;
}

And for return the value to the MasterPage:

master_page .value = "IDidSomething"; 
await App.Current.MainPage.Navigation.PopModalAsync();
WeakReferenceMessenger.Default.Send(master_page);

Greetings

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文