UWP数据绑定无法与ViewModel

时间:2017-02-22 23:53:34

标签: mvvm data-binding uwp inotifypropertychanged uwp-xaml

对于UWP和MVVM来说相当新的我遇到了一个问题,这对你们很多人来说都是显而易见的。

在我的项目中,我有3个名为ViewsViewModelsModels的文件夹,其中包含一些文件,如下图所示:

Can't upload image yet (reputation):

http://i.imgur.com/42f5KeT.png

  
    

问题:     我正在尝试实现MVVM。我已经搜索了几个小时的文章和视频,但似乎我总是遗漏一些东西。我在LoginPage.xaml中有一些绑定,然后我在Models/LoginPageModel.cs内的类中进行修改。我的LoginPageViewModel.cs中有一个INotifyPropertyChanged类,每次我的LoginPageModel.cs中的属性发生变化时,我希望INotifyPropertyChanged类触发,然后更改{{1}中的属性查看。下面我有这些文件的内容。

  

这是我的LoginPage.xaml代码的示例:

LoginPageModel.cs

这是我的using System; using System.Threading.Tasks; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; namespace App_Name.Models { class LoginPageModel { private NotifyChanges notify; public async void LogIn() { if (something is true) notify.LoginUIVisibility = Visibility.Visible; } } }

LoginPageViewModel.cs

以下是using System.ComponentModel; using System.Runtime.CompilerServices; using Windows.UI.Xaml; namespace App_Name.ViewModels { public class NotifyChanges : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } private Visibility loginUIVisibility = Visibility.Collapsed; public Visibility LoginUIVisibility { get { return loginUIVisibility; } set { if (value != loginUIVisibility) { loginUIVisibility = value; NotifyPropertyChanged("LoginUIVisibility"); } } } } } 的示例:

LoginPage.xaml

这是我的<Page x:Class="App_Name.LoginPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:App_Name" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:vm="using:App_Name.ViewModels" mc:Ignorable="d"> <Page.DataContext> <vm:NotifyChanges/> </Page.DataContext> <StackPanel Visibility="{Binding LoginUIVisibility}">

LoginPage.xaml.cs
  

我不知道为什么这不起作用。绑定用于不起作用,但现在在运行时它给了我一个未处理的异常,我认为它与不为namespace App_Name { public sealed partial class LoginPage : Page { private LoginPageModel login; public LoginPage() { InitializeComponent(); login.LogIn(); } } } private NotifyChanges notify对象分配任何值有关,但我不知道知道什么。提前感谢大家的时间!

如果您需要澄清我的问题,请写一个评论。谢谢!

1 个答案:

答案 0 :(得分:0)

  

以下是主要课程的变革要求:

     

NotifyChanges notifyChanges = new NotifyChanges();

     

notifyChanges.LoginUIVisibility = Visibility.Visible;

您已通过添加notifyChangesXAML文件中实例化<vm:NotifyChanges/>。并使用<StackPanel Visibility="{Binding LoginUIVisibility}">向StackPanel添加绑定。但是您创建了一个新的notifyChanges,并且未将新的notifyChanges绑定到StackPanel。所以它不会起作用。您可以像下面的代码一样初始化viewModel

<强>的MainPage

private LoginViewModel viewModel;

public MainPage()
 {
     this.InitializeComponent();
     viewModel = this.DataContext as LoginViewModel;
 }

 private void showDetail_Click(object sender, RoutedEventArgs e)
 {
     viewModel.LoginUIVisibility = Visibility.Visible;
 }

<强> MainPage.xaml中

<Page.DataContext>
    <vm:LoginViewModel />
</Page.DataContext>
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Button x:Name="loginButon" Content="Login" Visibility="{Binding LoginUIVisibility}" />
    <Button x:Name="showDetail" Content="Show" Click="showDetail_Click" />
</StackPanel>