我对UWP和MVVM还比较陌生,遇到了一个问题,可能对你们中的许多人来说很显然。
在我的项目中,我有3个文件夹,分别命名为
This is a sample of my
我不知道为什么这不起作用。绑定以前不起作用,但现在在运行时它给了我一个未处理的异常,我认为这与没有为私有的NotifyChanges notify和private LoginPageModel login对象分配任何值有关,但我不知道具体是什么原因。提前感谢大家的时间!如果您需要澄清我的问题,请写下评论。谢谢!
在我的项目中,我有3个文件夹,分别命名为
Views
、ViewModels
和Models
,其中包括一些文件,如下图所示:
目前还不能上传图片(声望不够):
http://i.imgur.com/42f5KeT.png
The problem: 我正在尝试实现MVVM。我已经花了几个小时查找文章和视频,但似乎总是缺少某些东西。我在LoginPage.xaml
中有一些绑定,然后在Models/LoginPageModel.cs
类中进行修改。我在我的LoginPageViewModel.cs
中有一个INotifyPropertyChanged
类,在LoginPageModel.cs
中每次属性更改时,我希望INotifyPropertyChanged
类触发,然后更改LoginPage.xaml
视图中的属性。下面是这些文件的内容。This is a sample of my
LoginPageModel.cs
code: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();
}
}
}
我不知道为什么这不起作用。绑定以前不起作用,但现在在运行时它给了我一个未处理的异常,我认为这与没有为私有的NotifyChanges notify和private LoginPageModel login对象分配任何值有关,但我不知道具体是什么原因。提前感谢大家的时间!如果您需要澄清我的问题,请写下评论。谢谢!