2013-10-06 187 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

相關問題