我試圖可見性屬性,以我在一個視圖模型(MainViewModel
)做了一個功能綁定,但我得到這個錯誤:綁定到另一個視圖模型
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll System.Windows.Data Error: BindingExpression path error: 'Main' property not found on 'Locator' 'System.String' (HashCode=-191326816). BindingExpression: Path='Main.TilesHomeViewVisible' DataItem='Locator' (HashCode=-191326816); target element is 'myApp.Views.TilesHomeView' (Name='myTilesHomeView'); target property is 'Visibility' (type 'System.Windows.Visibility')..
從我從錯誤理解,它正在尋找TilesHomeViewModel
中的TilesHomeViewVisible
函數,而它實際上在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>
The 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()
{
}
}
}
我希望這是儘可能詳細。我真的很希望找到解決這個錯誤的方法,現在它已經讓我煩惱了好幾天。
感謝
發佈頁面的.xaml,需要的是綁定的上下文,而不是綁定定義。 – lisp
請將後面的代碼貼出 – Florian
我已更新我的帖子,以更多代碼來詳細說明情況。請看一看。謝謝 – JNate