2013-10-06 132 views
0

我试图可见性属性,以我在一个视图模型(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() 
     { 
     } 
    } 
} 

我希望这是尽可能详细。我真的很希望找到解决这个错误的方法,现在它已经让我烦恼了好几天。

感谢

+0

发布页面的.xaml,需要的是绑定的上下文,而不是绑定定义。 – lisp

+0

请将后面的代码贴出 – Florian

+0

我已更新我的帖子,以更多代码来详细说明情况。请看一看。谢谢 – JNate

回答

0

的问题是,你可能是你在你的用户控件TilesHomeView的根目录设置为TilesHomeViewModel在DataContext(这样意味着TilesHomeView的DataContext的元素也TilesHomeView)。
下面是两种可能的解决了这个问题:

当设置能见度显式地设置源:

<local:TilesHomeView x:Name="myTilesHomeView" Visibility="{Binding Main.TilesHomeViewVisible,Source={StaticResource Locator} }" /> 

(更新与您定位合适的值)

-A第二解决方案是将DataContext设置为您UserControl中的TilesHomeViewModel的位置:将DataContext设置为User控件的LayoutRoot网格,而不是根目录。

+0

嗨,我用你提供给我的代码,但它仍然无法找到该功能。查看我的更新代码以获取错误消息,并更好地查看我的应用程序的类和页面 – JNate

+0

@JNate更新了我的答案,您需要使用{StaticResource Locator}而不是Locator(否则它将源绑定到字符串定位器而不是定位器资源 –

+0

它的工作!非常感谢! 我设法必须让TileshomeView出现并消失,但是我注意到,当瓷砖再次出现时,它不会再显示瓷砖内的数据。我的焦点是使用windows phone工具包创建的。你觉得我可能做错了什么?你认为我应该调用一个函数来重新加载数据,每次瓦片被回叫? – JNate

相关问题