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# MVVM中运行时的Onload绑定错误_C#_Wpf_Mvvm_Data Binding - Fatal编程技术网

C# MVVM中运行时的Onload绑定错误

C# MVVM中运行时的Onload绑定错误,c#,wpf,mvvm,data-binding,C#,Wpf,Mvvm,Data Binding,我尝试使用MVVM Light框架和“OnLoad”视图函数,以便在加载表单时执行ViewModel中的函数 我通过以下几个链接的示例实现了这一点: 我的看法是: <i:Interaction.Triggers> <i:EventTrigger EventName="Loaded"> <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding LoadWindowCommand

我尝试使用MVVM Light框架和“OnLoad”视图函数,以便在加载表单时执行ViewModel中的函数

我通过以下几个链接的示例实现了这一点:

我的看法是:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
        <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding LoadWindowCommand, Mode=OneWay}" />
    </i:EventTrigger>
</i:Interaction.Triggers>
它可以工作,但使用任何链接的方法,会在运行时引发绑定错误:

System.Windows.Data信息:10:无法使用绑定检索值,且不存在有效的回退值;改为使用默认值。BindingExpression:Path=LoadWindowCommand;DataItem=null;目标元素是“EventToCommand”(HashCode=4875788);目标属性为“Command”(类型为“ICommand”)

如何避免这个错误


更新,我已经按照您的建议更新了所有代码

主视图:

<Window x:Class="MvvmLight1.MainWindow"
        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:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
       xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:ignore="http://www.ignore.com"
        xmlns:local="clr-namespace:MvvmLight1.ViewModel"
        mc:Ignorable="d ignore"
        Title="MVVM Light Application" Height="300" Width="300">
    <Window.Resources>
        <local:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
    </Window.Resources>
    <Window.DataContext>
        <!--Move DataContext binding there-->
        <Binding Path="Main" Source="{StaticResource Locator}"/>
    </Window.DataContext>
    <Grid>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Loaded">
                <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding LoadWindowCommand, Mode=OneWay}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Grid>
</Window>
视图模型定位器

class ViewModelLocator
{
    public ViewModelLocator()
    {
        Main = new MainWindowViewModel();
    }

    public MainWindowViewModel Main { get; private set; }
}
我在App.xaml中评论了以下几行:

<!--<Application.Resources>
    <vm:ViewModelLocator x:Key="Locator"
                         d:IsDataSource="True" />
</Application.Resources>-->    
我能看到的代码的唯一区别在于主窗口的名称空间引用:

xmlns:mvvm_light_cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"
和我的:

xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
如果我添加您的名称空间,它将返回并在尝试解析“GalaSoft.MvvmLight.Platform”时出错


有什么建议吗?好的,我知道了。问题是您试图在定位器资源初始化之前获取它。只需将DataContext绑定放在参考资料部分之后。代码如下:

main window.xaml

<Window x:Class="MvvmLight1.MainWindow"
        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:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras"
       xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:ignore="http://www.ignore.com"
        xmlns:local="clr-namespace:MvvmLight1.ViewModel"
        mc:Ignorable="d ignore"
        Title="MVVM Light Application" Height="300" Width="300">
    <Window.Resources>
        <local:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
    </Window.Resources>
    <Window.DataContext>
        <!--Move DataContext binding there-->
        <Binding Path="Main" Source="{StaticResource Locator}"/>
    </Window.DataContext>
    <Grid>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Loaded">
                <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding LoadWindowCommand, Mode=OneWay}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Grid>
</Window>
ViewModelLocator.cs

namespace MvvmLight1.ViewModel
{
    public class MainViewModel : ViewModelBase
    {
        public MainViewModel()
        {
            LoadWindowCommand = new RelayCommand(OnLoadWindowCommand);
        }

        public RelayCommand LoadWindowCommand { get; private set; }
        private void OnLoadWindowCommand()
        {
            Debug.WriteLine("Loaded!");
        }
    }
}
namespace MvvmLight1.ViewModel
{
    public class ViewModelLocator
    {

        public ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
            SimpleIoc.Default.Register<MainViewModel>();
        }

        public MainViewModel Main
        {
            get
            {
                return ServiceLocator.Current.GetInstance<MainViewModel>();
            }
        }
    }
}
namespace MvvmLight1.ViewModel
{
公共类ViewModelLocator
{
公共ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(()=>SimpleIoc.Default);
SimpleIoc.Default.Register();
}
公共主视图模型主视图
{
得到
{
返回ServiceLocator.Current.GetInstance();
}
}
}
}

DataItem=null
表示没有数据上下文。是否将视图模型绑定到视图正确?其他绑定可以工作吗?是的,所有绑定都可以工作。为了避免问题,我在从Scratch开始的MVVM Light项目中添加了onload方法。ViewModel是在MainVindow“DataContext=“{Binding Main,Source={StaticResource Locator}}”的属性中设置的。我复制了您的所有代码和“Output”中的“窗口I有以下消息:System.Windows.Data信息:10:无法使用绑定检索值,并且不存在有效的回退值;改为使用默认值。BindingExpression:Path=LoadWindowCommand;DataItem=null;目标元素是“EventToCommand”(HashCode=7530847);目标属性已加载“命令”(类型“ICommand”)!好的,谢谢,现在清楚了。请查看更新的答案。它继续返回相同的错误。我在问题中加上了你的答案。我只看到名称空间的一个区别(您可以在我的问题中看到)。谢谢我注意到,MVVMlight在一个项目下创建了一个文件夹ViewModel,其中包含两个文件:MainViewModel.cs和ViewModelLocator.cs,默认情况下它们是空的。我在顶级命名空间中实现了MainWindowViewModel,而自动生成的MainViewModel和ViewModelLocator在MvvmLight1.ViewModel命名空间中。尝试将窗口标题中的xmlns:local=“clr namespace:MvvmLight1.ViewModel”更改为xmlns:local=“clr namespace:MvvmLight1”。我的MainWindowViewModel位于“MvvmLight1.ViewModel”命名空间中,而我的ViewModelLocator也位于“MvvmLight1.ViewModel”命名空间中。为了避免出现问题,我删除了有关ViewModelLocator和MainWindowViewModel的旧代码。
<Window x:Class="MvvmLight1.MainWindow"
        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:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras"
       xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:ignore="http://www.ignore.com"
        xmlns:local="clr-namespace:MvvmLight1.ViewModel"
        mc:Ignorable="d ignore"
        Title="MVVM Light Application" Height="300" Width="300">
    <Window.Resources>
        <local:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
    </Window.Resources>
    <Window.DataContext>
        <!--Move DataContext binding there-->
        <Binding Path="Main" Source="{StaticResource Locator}"/>
    </Window.DataContext>
    <Grid>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Loaded">
                <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding LoadWindowCommand, Mode=OneWay}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Grid>
</Window>
namespace MvvmLight1.ViewModel
{
    public class MainViewModel : ViewModelBase
    {
        public MainViewModel()
        {
            LoadWindowCommand = new RelayCommand(OnLoadWindowCommand);
        }

        public RelayCommand LoadWindowCommand { get; private set; }
        private void OnLoadWindowCommand()
        {
            Debug.WriteLine("Loaded!");
        }
    }
}
namespace MvvmLight1.ViewModel
{
    public class ViewModelLocator
    {

        public ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
            SimpleIoc.Default.Register<MainViewModel>();
        }

        public MainViewModel Main
        {
            get
            {
                return ServiceLocator.Current.GetInstance<MainViewModel>();
            }
        }
    }
}