簡體   English   中英

UWP C# MVVM 如何從其他頁面訪問 ViewModel

[英]UWP C# MVVM How To Access ViewModel from Other Page

我想通過一些示例場景進一步了解 MVVM。 我有一個帶有 ' rootpage ' textblock的根頁面。 我想通過激活任何形式的 UI 來顯示“狀態”或“場景”,例如。 ' togglebutton ' textblock上的切換按鈕。

我能夠將textblock中的頁面導航信息綁定到rootpageviewmodel 但是,當顯示來自不同頁面的信息時,我無法獲得結果。 我檢查了另一篇文章multiple-viewmodels-in-same-view & Accessing a property in a 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;
                    }
                    
                }
             }
        }
        

RootPage 視圖模型

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 如何從其他頁面訪問 ViewModel

更好的方法是為 RootPage 制作 static 變量,但不要為RootPageRootPageViewModel制作 singleton 實例。

例如:

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";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM