UWP C# MVVM 如何从其他页面访问 ViewModel

UWP C# MVVM How To Access ViewModel from Other Page

我想通过一些示例场景进一步了解 MVVM。我有一个 rootpage 和一个 'maindisplay' textblock。我想通过激活任何形式的 UI 例如显示 'status' 或 'scenarios'。 togglebutton 在 'maindisplay' textblock.

我能够将 rootpageviewmodel 中的页面导航信息绑定到 textblock。但是,当显示来自不同页面的信息时,我无法获得结果。 我检查了另一个 post & Accessing a property in one ViewModel from another 它非常相似但是没有用。

请帮忙。谢谢。

访问时RootPageViewModel应该保留实例吗?

查看

<TextBlock Text="{x:Bind RootViewModel.MainStatusContent, Mode=OneWay}"/>

RootPage.xaml.cs

        public sealed partial class RootPage : Page
        {
            private static RootPage instance;
            public RootPageViewModel RootViewModel { get; set; }
    
            public RootPage()
            {
                RootViewModel = new RootPageViewModel();
    
                this.InitializeComponent();
    
                // Always use the cached page
                this.NavigationCacheMode = NavigationCacheMode.Required;
            }
    
            public static RootPage Instance
            {
                get
                {
                    if (instance == null)
                    {
                        instance = new RootPage();
                    }
                    return instance;
                }
            }
         private void nvTopLevelNav_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
            {
                if (args.IsSettingsInvoked)
                {
                    contentFrame.Navigate(typeof(SettingsPage));
    
                    RootViewModel.MainStatusContent = "Settings_Page";
                }
                else
                { 
                    var navItemTag = args.InvokedItemContainer.Tag.ToString();        
                    
                    RootViewModel.MainStatusContent = navItemTag;
                                     
                    switch (navItemTag)
                    {
                        case "Home_Page":   
                            contentFrame.Navigate(typeof(HomePage));
                            break;
    
                        case "Message_Page":  
                            contentFrame.Navigate(typeof(MessagePage));
                            break;
                    }
                    
                }
             }
        }
        

根页面视图模型:

public class RootPageViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private static RootPageViewModel instance = new RootPageViewModel();
    public static RootPageViewModel Instance
    {
        get
        {
            if (instance == null)
                instance = new RootPageViewModel();

            return instance;
        }
    }

    public RootPageViewModel()
    {
    }

    private string _mainStatusContent;
    public string MainStatusContent
    {
        get
        {
            return _mainStatusContent;
        }
        set
        {
            _mainStatusContent = value;
            OnPropertyChanged();
        }
    }

    protected void OnPropertyChanged([CallerMemberName] string name = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}

MessagePage.xaml.cs - 访问 RootPage ViewModel

public sealed partial class MessagePage : Page
{
    public MessagePageViewModel MessageViewModel { get; set; }

    public MessagePage()
    {
        MessageViewModel = new MessagePageViewModel();

        this.InitializeComponent();

        // Always use the cached page
        this.NavigationCacheMode = NavigationCacheMode.Required;
    }

    private void Message1_Checked(object sender, RoutedEventArgs e)
    {
        RootPageViewModel.Instance.MainStatusContent = "Message 1 Selected";
    }

    private void Message1_Unchecked(object sender, RoutedEventArgs e)
    {
        RootPageViewModel.Instance.MainStatusContent = "Message 1 De-Selected";
    }
}

当我调试时,值确实写入了实例但没有更新 TextBlock。我在 XAML 绑定中做错了什么吗?

UWP C# MVVM How To Access ViewModel from Other Page

更好的方法是为 RootPage 创建静态变量,而不是为 RootPageRootPageViewModel.

创建单例实例

例如:

public RootPage ()
{
    this.InitializeComponent();
    this.NavigationCacheMode = NavigationCacheMode.Required;
    Instance = this;
    RootViewModel = new RootPageViewModel();
}

public static RootPage Instance;

用法

private void Message1_Checked(object sender, RoutedEventArgs e)
{

    RootPage.Instance.RootViewModel.MainStatusContent = "Message 1 Selected";
}

private void Message1_Unchecked(object sender, RoutedEventArgs e)
{
    RootPage.Instance.RootViewModel.MainStatusContent = "Message 1 De-Selected";
}