Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/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
Silverlight 4.0 关闭视图后,ViewModel仍然存在_Silverlight 4.0_Mvvm Light_Messenger - Fatal编程技术网

Silverlight 4.0 关闭视图后,ViewModel仍然存在

Silverlight 4.0 关闭视图后,ViewModel仍然存在,silverlight-4.0,mvvm-light,messenger,Silverlight 4.0,Mvvm Light,Messenger,我有一个用户控制窗口,任务是添加新项。 我从父窗口调用此窗口,但当我关闭此模式控件并再次调用它时,我看到viewmodel与以前相同,因此控件中填充了旧数据。当然,我需要有空字段的控件: 我用的是MVVM灯。我知道这可能与Messenger有关,但我没有发现我的代码有任何问题 <telerik:RadWindow x:Class="MyAppApp.View.AddMessageTemplateView" xmlns="http://schemas.microso

我有一个用户控制窗口,任务是添加新项。 我从父窗口调用此窗口,但当我关闭此模式控件并再次调用它时,我看到viewmodel与以前相同,因此控件中填充了旧数据。当然,我需要有空字段的控件:

我用的是MVVM灯。我知道这可能与Messenger有关,但我没有发现我的代码有任何问题

<telerik:RadWindow x:Class="MyAppApp.View.AddMessageTemplateView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                   xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
                   xmlns:prismvm="clr-namespace:Microsoft.Practices.Prism.ViewModel;assembly=Microsoft.Practices.Prism"
                   xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
                   xmlns:Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.SL4"
                   xmlns:Converters="clr-namespace:MyAppApp.Converters"

                   xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
                   xmlns:HTMLParsing="clr-namespace:MyAppApp.Helpers.HTMLParsing" mc:Ignorable="d"
             DataContext="{Binding AddMessageTemplate, Source={StaticResource Locator}}"  d:DesignHeight="300" d:DesignWidth="400" WindowStartupLocation="CenterScreen"
     ResizeMode="NoResize" Width="355" >
    <telerik:RadWindow.Resources>
        <Converters:RadioButtonConverter x:Key="RadioButtonConverter"/>
        <Converters:EnumsMatchToVisibilityConverter x:Key="EnumsMatchToVisibilityConverter"/>
    </telerik:RadWindow.Resources>
    <Grid >
        <Grid.RowDefinitions>
            <RowDefinition Height="20"/>
            <RowDefinition Height="20"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="40"/>
        </Grid.RowDefinitions>
        <TextBlock Text="{Binding Path=MyAppResources.label_PleaseSelectTypeOfMessageTemplate, Source={StaticResource ResourcesWrapper}}"/>
        <StackPanel Grid.Row="1" Orientation="Horizontal">
            <RadioButton Content="Email" Margin="0 0 10 0" 
  GroupName="MessageTypes" 
  IsChecked="{Binding SelectedType, Mode=TwoWay, ConverterParameter=Email, 
             Converter={StaticResource RadioButtonConverter}}" />

            <RadioButton Content="SMS" Margin="0 0 10 0" 
  GroupName="MessageTypes" 
  IsChecked="{Binding SelectedType, Mode=TwoWay, ConverterParameter=Sms, 
             Converter={StaticResource RadioButtonConverter}}" />
        </StackPanel>
        <!--Place for New Item> -->
        <StackPanel Grid.Row="2" HorizontalAlignment="Stretch">
            <Grid
    x:Name="DetailsGrid">
                <prismvm:DataTemplateSelector
        Content="{Binding NewTemplate}" HorizontalContentAlignment="Stretch">
                    <prismvm:DataTemplateSelector.Resources>
                        <DataTemplate
                x:Key="EmailMessageTemplate">
                            <StackPanel Orientation="Vertical">
                                <TextBlock Text="{Binding Path=MyAppResources.label_NameOfMessageTemplate, Source={StaticResource ResourcesWrapper}}"/>
                                <TextBox Text="{Binding Name,Mode=TwoWay}" HorizontalAlignment="Stretch" />
                                <TextBlock Text="{Binding Path=MyAppResources.label_SubjectOfMessageTemplate, Source={StaticResource ResourcesWrapper}}"/>
                                <TextBox Text="{Binding EmailSubject,Mode=TwoWay}" HorizontalAlignment="Stretch"/>

                                <StackPanel Orientation="Vertical">
                                    <RadioButton Content="HTML" Margin="0 0 10 0" 
  GroupName="EmailFormat" 
  IsChecked="{Binding EmailFormat, Mode=TwoWay, ConverterParameter=HTML, 
             Converter={StaticResource RadioButtonConverter}}" />

                                    <RadioButton Content="Plain Text" Margin="0 0 10 0" 
  GroupName="EmailFormat" x:Name="PlainTextRadioButton" 
  IsChecked="{Binding EmailFormat, Mode=TwoWay, ConverterParameter=PlainText, 
             Converter={StaticResource RadioButtonConverter}}" />
                                </StackPanel>
                                <TextBlock Text="{Binding Path=MyAppResources.label_BodyOfMessageTemplate, Source={StaticResource ResourcesWrapper}}"/>

                                <ScrollViewer MaxHeight="100" HorizontalAlignment="Stretch" Margin="0,10,0,10" Name="scrollViewerEmailBody" VerticalAlignment="Top">
                                    <TextBox Text="{Binding Body,Mode=TwoWay}" TextWrapping="Wrap" AcceptsReturn="True" />
                                </ScrollViewer>

                                <ScrollViewer  HorizontalAlignment="Stretch" Margin="0,10,0,10" Name="scrollViewerEmail2RadHtmlPlaceholder" Visibility="{Binding EmailFormat,ConverterParameter=HTML,Converter={StaticResource  EnumsMatchToVisibilityConverter}}" >
                                    <HTMLParsing:HtmlTextBlock Html="{Binding Body}"/>
                                </ScrollViewer>
                            </StackPanel>

                        </DataTemplate>
                        <DataTemplate
                x:Key="SmsMessageTemplate">
                            <StackPanel Orientation="Vertical">
                                <TextBlock Text="{Binding Path=MyAppResources.label_NameOfMessageTemplate, Source={StaticResource ResourcesWrapper}}"/>
                                <TextBox Text="{Binding Name,Mode=TwoWay}" HorizontalAlignment="Stretch" />
                                <TextBlock Text="{Binding Path=MyAppResources.label_BodyOfMessageTemplate, Source={StaticResource ResourcesWrapper}}"/>

                                <ScrollViewer MaxHeight="100" HorizontalAlignment="Stretch" Margin="0,10,0,10"  VerticalAlignment="Top">
                                    <TextBox Text="{Binding Body,Mode=TwoWay}" TextWrapping="Wrap" AcceptsReturn="True" />
                                </ScrollViewer>
                            </StackPanel>
                        </DataTemplate>

                    </prismvm:DataTemplateSelector.Resources>
                </prismvm:DataTemplateSelector>
            </Grid>
        </StackPanel>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"
              Grid.Row="3" Grid.Column="0">
            <telerik:RadButton x:Name="btnOK" Width="120" Content="{Binding Path=MyAppResources.OkSoeDialog, Source={StaticResource ResourcesWrapper}}"
                 Margin="5"  >
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        <Command:EventToCommand Command="{Binding Source={StaticResource Locator}, Path= AddMessageTemplate.SaveCommand}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </telerik:RadButton>
            <telerik:RadButton x:Name="btnCancel" Width="120" Content="{Binding Path=MyAppResources.CancelSoeDialog, Source={StaticResource ResourcesWrapper}}"
                 Margin="5"  >
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        <Command:EventToCommand Command="{Binding Source={StaticResource Locator}, Path= AddMessageTemplate.CancelCommand}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>

            </telerik:RadButton>
        </StackPanel>
    </Grid>
</telerik:RadWindow>
和父控件的一段代码

private void HandleNotification(NotificationMessage obj)
        {
            if (obj.Notification == "ShowAddMessageTemplate")
            {
                var ShowAddMessageTemplateRadWindow = new AddMessageTemplateView();
                ShowAddMessageTemplateRadWindow.Show();
            }
        }
父控件的一个模型:

private void ShowAddMessageTemplate()
        {


            Messenger.Default.Send(new NotificationMessage(this, "ShowAddMessageTemplate"));


            Messenger.Default.Register<NotificationMessage<MessageTemplate>>(this, HandleMessageTemplateAdding);


        }

        private void HandleMessageTemplateAdding(NotificationMessage<MessageTemplate> notificationMessage)
        {

            if (notificationMessage.Notification=="Add")
            {
                ServiceAgent.AddMessageTemplate(notificationMessage.Content,_App.HandlerId,GetMessageTemplates);
            }

        }

我通过向AddMessageTemplateView的构造函数添加以下内容来解决该问题:

this.Closed += (s, e) => ViewModelLocator.ClearAddMessageTemplate();
ClearAddMessageTemplateI是:

this.Closed += (s, e) => ViewModelLocator.ClearAddMessageTemplate();
  public static void ClearAddMessageTemplate()
        {
            _addMessageTemplate.Cleanup();
            _addMessageTemplate = null;
        }