Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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# 窗口加载和WPF_C#_Wpf_Events_Xaml_Mvvm - Fatal编程技术网

C# 窗口加载和WPF

C# 窗口加载和WPF,c#,wpf,events,xaml,mvvm,C#,Wpf,Events,Xaml,Mvvm,我在Windows 2012中有一个WPF项目,其中我需要在Window Loaded事件中加载一些信息。不过,我需要在视图模型中而不是在代码隐藏中执行此操作。我正在尝试使用以下代码: 在我的xaml中: <interactivity:Interaction.Behaviors> <behaviors:WindowLoadedBehavior LoadedCommand="{Binding WindowLoadedCommand}" /> </interac

我在Windows 2012中有一个WPF项目,其中我需要在Window Loaded事件中加载一些信息。不过,我需要在视图模型中而不是在代码隐藏中执行此操作。我正在尝试使用以下代码:

在我的xaml中:

<interactivity:Interaction.Behaviors>
    <behaviors:WindowLoadedBehavior LoadedCommand="{Binding WindowLoadedCommand}" />
</interactivity:Interaction.Behaviors>
我的附属行为:

public class WindowLoadedBehavior : Behavior<FrameworkElement>
{
    [SuppressMessage("Microsoft.StyleCop.CSharp.MaintainabilityRules", "SA1401:FieldsMustBePrivate", Justification = "Dependency Property.  Allow public.")]
    public static DependencyProperty LoadedCommandProperty = DependencyProperty.Register("LoadedCommand", typeof(ICommand), typeof(WindowLoadedBehavior), new PropertyMetadata(null));

    public ICommand LoadedCommand
    {
        get { return (ICommand)GetValue(LoadedCommandProperty); }
        set { SetValue(LoadedCommandProperty, value); }
    }

    protected override void OnAttached()
    {
        base.OnAttached();

        AssociatedObject.Loaded += AssociatedObject_Loaded;
    }

    protected override void OnDetaching()
    {
        AssociatedObject.Loaded -= AssociatedObject_Loaded;

        base.OnDetaching();
    }

    private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
    {
        if (LoadedCommand != null)
            LoadedCommand.Execute(null);
    }
}
公共类WindowLoadedBehavior:行为
{
[SuppressMessage(“Microsoft.StyleCop.CSharp.maintabilityRules”,“SA1401:FieldsMustBePrivate”,justionment=“Dependency Property.Allow public.”)
public static dependencProperty LoadedCommandProperty=dependencProperty.Register(“LoadedCommand”、typeof(ICommand)、typeof(WindowLoadedBehavior)、new PropertyMetadata(null));
公共ICommand loaded命令
{
get{return(ICommand)GetValue(LoadedCommandProperty);}
set{SetValue(LoadedCommandProperty,value);}
}
受保护的覆盖无效附加()
{
base.onatached();
AssociatedObject.Loaded+=AssociatedObject\u Loaded;
}
附加时受保护的覆盖无效()
{
AssociatedObject.Loaded-=AssociatedObject\u Loaded;
base.OnDetaching();
}
已加载关联对象的私有无效(对象发送方,路由目标)
{
if(LoadedCommand!=null)
LoadedCommand.Execute(空);
}
}

OnAttached、AssociatedObject_Loaded和LoadedCommand get均处于激活状态,但LoadedCommand集未处于激活状态,显然,WindowLoadedCommand未处于激活状态。你知道我能做些什么来让它工作吗?

有几个选择。这里列出了其中的几个:

然而,如果你或其他人担心你花了几个小时来完成一项本应花费30秒的任务,那么你可能想试试这个

public MainWindow()
{
    InitializeComponent();

    this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    ShellViewModel.Instance.WindowLoadedCommand.Execute(null);
}

您没有直接绑定到命令有什么特别的原因吗?据我所知,由于某些原因,直接绑定窗口加载的事件不起作用。这太完美了!显然,我遇到的问题是在加载窗口之前没有正确加载事件处理类。你的方法(加上短小精悍)避免了这种情况,并允许它正确地发射。
public MainWindow()
{
    InitializeComponent();

    this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    ShellViewModel.Instance.WindowLoadedCommand.Execute(null);
}