Silverlight:MVVM 和重新初始化表单

发布于 2024-10-10 19:55:25 字数 1004 浏览 4 评论 0原文

我们使用 Prism,从网格中弹出一个编辑表单,其中有两个选项:“保存”和“保存并新建”。我的问题是关于重新初始化表单。我想知道是否有更好或更简单的方法?我所做的是在视图模型上公开 InteractionRequest,然后在 xaml 中使用 InteractionRequestTrigger 来更改表单的属性,如下所示:

private void SubmitAndNewCommandCallback(IEnumerable<ValidationResult> errors)
{
    if (errors != null && errors.Any())
    {
        Errors = errors.Select(x => x.ErrorMessage).ToList();
    }
    else
    {
        if (IsNew)
        {
            _events.GetEvent<BidAgentCreated>().Publish(this.BidAgent);
        }

        _intializeFormRequest.Raise(this);
    } 
}


<i:Interaction.Triggers>
    <prism:InteractionRequestTrigger SourceObject="{Binding InitializeFormRequest}"  >
        <ei:ChangePropertyAction TargetName="ctlAgentType" PropertyName="SelectedIndex" Value="0" />
        <ei:ChangePropertyAction TargetName="ctlAgentSearchBox" PropertyName="Text" Value=""/>
    </prism:InteractionRequestTrigger>
</i:Interaction.Triggers>

We use Prism and from the grid we pop up a edit form that has two options, "Save" and "Save and New". My question is about re-initializing form. I am wondering if there is a better or simpler way? What I do is expose a InteractionRequest on the view model, and than use InteractionRequestTrigger in xaml to change the properties of the form, like this:

private void SubmitAndNewCommandCallback(IEnumerable<ValidationResult> errors)
{
    if (errors != null && errors.Any())
    {
        Errors = errors.Select(x => x.ErrorMessage).ToList();
    }
    else
    {
        if (IsNew)
        {
            _events.GetEvent<BidAgentCreated>().Publish(this.BidAgent);
        }

        _intializeFormRequest.Raise(this);
    } 
}


<i:Interaction.Triggers>
    <prism:InteractionRequestTrigger SourceObject="{Binding InitializeFormRequest}"  >
        <ei:ChangePropertyAction TargetName="ctlAgentType" PropertyName="SelectedIndex" Value="0" />
        <ei:ChangePropertyAction TargetName="ctlAgentSearchBox" PropertyName="Text" Value=""/>
    </prism:InteractionRequestTrigger>
</i:Interaction.Triggers>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话已封尘 2024-10-17 19:55:25

一种干净的方法是摆脱 View 中的逻辑并将其保留在 ViewModel 中。

xaml 中

<ComboBox ItemsSource="{Binding AgentTypes}" SelectedItem="{Binding SelectedAgentType,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"/>
<TextBox Text="{Binding SearchText,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" />

在 ViewModel 的

private void SubmitAndNewCommandCallback(IEnumerable<ValidationResult> errors)
{
    if (errors != null && errors.Any())
    {
        Errors = errors.Select(x => x.ErrorMessage).ToList();
    }
    else
    {
        if (IsNew)
        {
            _events.GetEvent<BidAgentCreated>().Publish(this.BidAgent);
        }

        SearchText="";
        SelectedAgentType = AgentTypes.First();  //selects first agenttype, or set to null to select nothing in the combobox

    } 
}

A clean way is to get rid of the logic in your View and keep it in the ViewModel.

in xaml

<ComboBox ItemsSource="{Binding AgentTypes}" SelectedItem="{Binding SelectedAgentType,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"/>
<TextBox Text="{Binding SearchText,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" />

in the ViewModel

private void SubmitAndNewCommandCallback(IEnumerable<ValidationResult> errors)
{
    if (errors != null && errors.Any())
    {
        Errors = errors.Select(x => x.ErrorMessage).ToList();
    }
    else
    {
        if (IsNew)
        {
            _events.GetEvent<BidAgentCreated>().Publish(this.BidAgent);
        }

        SearchText="";
        SelectedAgentType = AgentTypes.First();  //selects first agenttype, or set to null to select nothing in the combobox

    } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文