我正在尝试将可见性属性绑定到我在viewmodel(MainViewModel
)中创建的函数,但是我收到此错误:
mscorlib.dll中出现'System.IO.FileNotFoundException'类型的第一次机会异常 System.Windows.Data错误:BindingExpression路径错误:'Locator''System.String'上找不到'Main'属性(HashCode = -191326816)。 BindingExpression:Path ='Main.TilesHomeViewVisible'DataItem ='Locator'(HashCode = -191326816); target元素是'myApp.Views.TilesHomeView'(Name ='myTilesHomeView'); target属性是'Visibility'(类型'System.Windows.Visibility')..
根据我从错误中理解的内容,它正在寻找TilesHomeViewVisible
中的TilesHomeViewModel
函数,而它实际上位于MainViewModel
中。在绑定表达式中,如何定位MainViewModel
呢?
编辑:我整合了“ViewModelLocator”。
这是我的ViewModelLocator:
...
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
SimpleIoc.Default.Register<MainViewModel>();
SimpleIoc.Default.Register<TilesHomeViewModel>();
}
public MainViewModel Main
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
}
public TilesHomeViewModel TilesVM
{
get
{
return ServiceLocator.Current.GetInstance<TilesHomeViewModel>();
}
}
...
我的App.xaml:
<?xml version="1.0" encoding="utf-8"?>
<Application x:Class="myApp.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:app="clr-namespace:myApp" mc:Ignorable="d"
xmlns:views="clr-namespace:myApp.Views"
xmlns:vm="clr-namespace:myApp.ViewModels">
<!--Application Resources-->
<Application.Resources>
<ResourceDictionary>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<app:ColorWrapper x:Key="ColorWrapper" />
</ResourceDictionary>
<ResourceDictionary x:Name="ResourceDictionary1" Source="ResourceDictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
<Application.ApplicationLifetimeObjects>
<!--Required object that handles lifetime events for the application-->
<shell:PhoneApplicationService Launching="Application_Launching" Closing="Application_Closing" Activated="Application_Activated" Deactivated="Application_Deactivated" />
</Application.ApplicationLifetimeObjects>
</Application>
在我的MainPage.xaml和链接到定位器的地方,我有:
<phone:PhoneApplicationPage
...
d:DataContext="{d:DesignData SampleData/MainViewModelSampleData.xaml}"
...
>
<phone:PhoneApplicationPage.DataContext>
<Binding Path="Main" Source="{StaticResource Locator}"/>
</phone:PhoneApplicationPage.DataContext>
...
<Grid x:Name="LayoutRoot">
...
<local:TilesHomeView x:Name="myTilesHomeView" Visibility="{Binding Main.TilesHomeViewVisible,Source=Locator}" />
</Grid>
</phone:PhoneApplicationPage>
MainViewModel.cs
public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
this.Items = new ObservableCollection<ItemViewModel>();
}
Visibility _tilesHomeViewVisible = System.Windows.Visibility.Collapsed;
public Visibility TilesHomeViewVisible
{
get { return System.Windows.Visibility.Collapsed; }
set { _tilesHomeViewVisible = value; RaisePropertyChanged("TilesHomeViewVisible"); }
}
public void TilesHomeViewClose()
{
TilesHomeViewVisible = System.Windows.Visibility.Collapsed;
}
public bool IsDataLoaded
{
get;
private set;
}
/// <summary>
/// Creates and adds a few ItemViewModel objects into the Items collection.
/// </summary>
public void LoadData()
{...}
}
TilesHomeView.xaml将数据上下文定义为:
<UserControl x:Class="myApp.Views.TilesHomeView"
....
DataContext="{Binding TilesVM, Source={StaticResource Locator}}"
>
<Grid x:Name="HomeGrid">
</Grid>
</UserControl>
HomeViewModel.cs没有任何功能,如此显示
namespace myApp
{
public class TilesHomeViewModel : ViewModelBase
{
public TilesHomeViewModel()
{
}
}
}
我希望尽可能详细。我真的希望找到一个解决这个错误的方法,它已经困扰了我好几天了。
由于
答案 0 :(得分:0)
问题是您可能正在将TilesHomeViewModel设置为TilesHomeView用户控件的根目录上的DataContext(这意味着TilesHomeView元素的DataContext也是TilesHomeView)。
以下是此问题的两种可能解决方案:
设置可见性时明确设置源:
<local:TilesHomeView x:Name="myTilesHomeView" Visibility="{Binding Main.TilesHomeViewVisible,Source={StaticResource Locator} }" />
(使用定位器上的相应值进行更新)
- 第二个解决方案是在UserControl中将DataContext设置为TilesHomeViewModel:将DataContext设置为User控件的LayoutRoot网格而不是root。