0

我正在尝试将可见性属性绑定到我在视图模型 ( MainViewModel) 中创建的函数,但出现此错误:

在 mscorlib.dll System.Windows.Data 错误中出现“System.IO.FileNotFoundException”类型的第一次机会异常:BindingExpression 路径错误:在“Locator”“System.String”上找不到“Main”属性(HashCode=-191326816) . BindingExpression: Path='Main.TilesHomeViewVisible' DataItem='Locator' (HashCode=-191326816); 目标元素是'myApp.Views.TilesHomeView'(名称='myTilesHomeView');目标属性是“可见性”(类型“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()
        {
        }
    }
}

我希望这尽可能详细。我真的希望找到解决此错误的方法,这几天一直困扰着我。

谢谢

4

1 回答 1

0

问题是您可能正在将 TilesHomeViewModel 设置为 TilesHomeView 用户控件根上的 DataContext (这意味着 TilesHomeView 元素的 DataContext 也是 TilesHomeView )。
对于这个问题,这里有两种可能的解决方案:

设置可见性时,明确设置源:

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

(使用定位器上的适当值进行更新)

- 第二种解决方案是移动您在 UserControl 中将 DataContext 设置为 TilesHomeViewModel 的位置:将 DataContext 设置为用户控件的 LayoutRoot 网格而不是根。

于 2013-10-06T19:15:42.107 回答