Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# mvvm windows phone 8混乱以及如何在文本更改时捕获_C#_Mvvm_Windows Phone 8 - Fatal编程技术网

C# mvvm windows phone 8混乱以及如何在文本更改时捕获

C# mvvm windows phone 8混乱以及如何在文本更改时捕获,c#,mvvm,windows-phone-8,C#,Mvvm,Windows Phone 8,我正在尝试使用mvvm模式编写一个WindowsPhone8应用程序,但我正在努力解决这个问题 我有一个页面,其中包含绑定到PersonViewModel的人员列表。那部分很好用。然后,我在应用程序栏中有两个按钮,即添加或编辑。当我想要编辑一个人时,我从列表中选择这个人,然后在我的ViewModel中设置CurrentPerson。这将在my MainViewModel中设置一个属性,用于存储当前选定的人员,即 App.MainViewModel.CurrentPerson = this.Cur

我正在尝试使用mvvm模式编写一个WindowsPhone8应用程序,但我正在努力解决这个问题

我有一个页面,其中包含绑定到PersonViewModel的人员列表。那部分很好用。然后,我在应用程序栏中有两个按钮,即添加或编辑。当我想要编辑一个人时,我从列表中选择这个人,然后在我的ViewModel中设置CurrentPerson。这将在my MainViewModel中设置一个属性,用于存储当前选定的人员,即

App.MainViewModel.CurrentPerson = this.CurrentPerson;
当我想添加一个新的人物时,我使用相同的主体,但我创建了一个新的人物模型

App.MainViewModel.CurrentPerson = new PersonModel();
然后,我重定向到一个页面,该页面包含处理个人的字段,无论是添加还是编辑,该字段都绑定到名为PersonEntryViewModel的ViewModel

在我解释我的问题之前,我想让你知道我在努力实现什么。我希望我的应用程序栏中的“保存”按钮能够在满足一定数量的条件后启用,即姓名已填写且包含x个字符等

我知道我的问题是什么,但我不知道如何解决它

以下是我的PersonEntryViewModel的简化版本:

public class PersonEntryViewModel : BaseViewModel
{
    private PersonModel _currentPerson;
    private bool _isNewPerson;
    private ICommand _savePersonCommand;
    private ICommand _cancelCommand;
    private ICommand _titleTextChanged;

    private bool _enableSaveButton;

    public PersonEntryViewModel()
    {
        this.CurrentPerson = App.MainViewModel.CurrentPerson ?? new PersonModel();
    }

    public ICommand SavePersonCommand
    {
        get
        {
            return this._savePersonCommand ?? (this._savePersonCommand = new DelegateCommand(SavePersonAction));
        }
    }

    public ICommand CancelCommand
    {
        get
        {
            return this._cancelCommand ?? (this._cancelCommand = new DelegateCommand(CancelAction));
        }
    }

    public ICommand NameTextChanged
    {
        get
        {
            return this._nameTextChanged ?? (this._nameTextChanged = new DelegateCommand(NameTextChangedAction));
        }
    }

    private void NameTextChangedAction(object actionParameters)
    {
        if (!string.IsNullOrEmpty(this._currentPerson.Name) && _currentPerson.Name.Length > 2)
        {
            EnableSaveButton = true;
        }            
    }

    private void CancelAction(object actionParameters)
    {
        Console.WriteLine("Cancel");
        INavigationService navigationService = this.GetService<INavigationService>();
        if (navigationService == null)
            return;
        navigationService.GoBack();
        navigationService = null;
    }

    private void SavePersonAction(object actionParameters)
    {
        Console.WriteLine("Saving");
    }

    public PersonModel CurrentPerson
    {
        get { return this._currentPerson; }
        set
        {
            if (this._currentPerson != value)
                this.SetProperty(ref this._currentPerson, value);
        }
    }

    public string PageTitle
    {
        get { return this._pageTitle; }
        set { if (this._pageTitle != value) this.SetProperty(ref this._pageTitle, value); }
    }

    public bool IsNewPerson
    {
        get { return this._isNewPerson; }
        set
        {
            if (this._isNewPerson != value)
            {
                this.SetProperty(ref this._isNewPerson, value);
                if (this._isNewPerson)
                    this.PageTitle = AppResources.PersonEntryPageNewTitle;
                else
                    this.PageTitle = AppResources.PersonEntryPageEditTitle;
            }
        }
    }

    public bool EnableSaveButton
    {
        get { return this._enableSaveButton; }
        set { if (this._enableSaveButton != value) this.SetProperty(ref this._enableSaveButton, value); }
    }
}
它会相应地被触发,但如何将此信息返回到绑定到视图的PersonEntryViewModel,其中包含我的两个按钮(即保存和取消)位于,并且EnableSaveButton属性负责在设置时相应地启用保存按钮(假设名称有效,例如set和minlen匹配)

PersonEntryViewModel和使用CurrentPerson属性以及正在编辑或添加的当前人员是否设计正确?我如何处理此场景

我希望上述内容有意义,但如果我对某些事情不清楚,请告诉我,我会尽力澄清

谢谢


PS:我发布了另一篇关于如何检测文本变化的帖子,但我找到了答案,但这显然不是问题所在。这个问题似乎更多地与设计有关。

我不清楚你的设计

如果你想使用当前的设计本身,我建议你做以下事情

删除在xaml中为网格分配DataContext

在代码隐藏中添加:

var dataContext = new PersonEntryViewModel();
this.ContentPanel.DataContext = dataContext.CurrentPerson;
// After creating App bar
this.appBar.DataContext = dataContext;
//Your xaml code will look something like this:
<AppBar x:Name="appBar">
    <Button x:Name="saveBtn" IsEnabled={Binding EnableSaveButton} />
<AppBar />
var dataContext=newpersonentryviewmodel();
this.ContentPanel.DataContext=DataContext.CurrentPerson;
//创建应用程序栏后
this.appBar.DataContext=DataContext;
//您的xaml代码如下所示:

您正在以代码隐藏方式创建应用程序条,对吗?不,我正在使用带有Xaml的cimbalino控件,它已绑定到我的viewmodel。我已更新了答案。看看它是否解决了问题。
<i:Interaction.Triggers>
    <i:EventTrigger EventName="TextChanged">
        <i:InvokeCommandAction Command="{Binding NameTextChanged, Mode=OneWay}" CommandParameter="{Binding}" />
    </i:EventTrigger>
</i:Interaction.Triggers>
private ICommand _personTextChanged;

public ICommand PersonTextChanged
{
   get
   {
       return this._personTextChanged ?? (this._personTextChanged = new DelegateCommand(PersonTextChangedAction));
   }
}

private void PersonTextChangedAction(object actionParameters)
{
    if (!string.IsNullOrEmpty(this._name) && this._name.Length > 2)
    {
        //EnableSaveButton = true;
        Console.WriteLine("");
    }
}
var dataContext = new PersonEntryViewModel();
this.ContentPanel.DataContext = dataContext.CurrentPerson;
// After creating App bar
this.appBar.DataContext = dataContext;
//Your xaml code will look something like this:
<AppBar x:Name="appBar">
    <Button x:Name="saveBtn" IsEnabled={Binding EnableSaveButton} />
<AppBar />