C# Silverlight MVVM实现不显示内容

C# Silverlight MVVM实现不显示内容,c#,wpf,silverlight,xaml,mvvm,C#,Wpf,Silverlight,Xaml,Mvvm,编辑: 问题是我的视图模型不是公共的,因此找不到绑定。出于某种原因,它们需要在Silverlight中公开,即使私有视图模型适用于WPF。更多细节请参见下面我的回答 我试图在Silverlight中实现一个MVVM WPF应用程序,但我以前从未使用过Silverlight。我正在努力让绑定的内容显示出来。我几乎完全按照我使用WPF的方式来实现它,除了主要部分是一个用户控件,而不是一个窗口,应用程序由一个网站托管,并且我必须删除DataTemplate中的x:TypeDataType 我的网站到

编辑: 问题是我的视图模型不是公共的,因此找不到绑定。出于某种原因,它们需要在Silverlight中公开,即使私有视图模型适用于WPF。更多细节请参见下面我的回答


我试图在Silverlight中实现一个MVVM WPF应用程序,但我以前从未使用过Silverlight。我正在努力让绑定的内容显示出来。我几乎完全按照我使用WPF的方式来实现它,除了主要部分是一个用户控件,而不是一个窗口,应用程序由一个网站托管,并且我必须删除
DataTemplate中的
x:Type
DataType

我的网站到主页正常工作,因为我可以显示一个文本框或在该页上的东西,但绑定只是不显示。有什么建议吗

My MainPage.xaml.cs:

public partial class MainPage : UserControl
{
    private MainPageViewModel _viewModel;

    public MainPage()
    {
        InitializeComponent();

        _viewModel = new MainPageViewModel();
        this.DataContext = _viewModel;
    }
}
My MainPage.xaml:

<UserControl x:Class="TransformationServices.Silverlight.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"
    xmlns:view="clr-namespace:TransformationServices.Silverlight.View"
    xmlns:viewModel="clr-namespace:TransformationServices.Silverlight.ViewModel"
    xmlns:local="clr-namespace:TransformationServices.Silverlight"
    Background="#FF2D2D30">
    <UserControl.Resources>
    <local:ViewConverter x:Key="viewConverter"/>
        <DataTemplate DataType="viewModel:InputSelectViewModel">
            <TextBlock Text="Hello World!"/>
            <!-- This textblock is just for testing. It doesn't work, and neither does the following line-->
            <view:InputSelect/>
        </DataTemplate>
    </UserControl.Resources>
    <ContentControl Content="{Binding CurrentView}" />
</UserControl>

我的主页ViewModel.cs

class MainPageViewModel : ViewModelBase, INotifyPropertyChanged
{ 
    private ViewModelBase _currentView;

    private List<ViewModelBase> _viewModels;

    private int _viewIndex;

    public ViewModelBase CurrentView
    {
        get { return _currentView; }
        set
        {
            if (value != _currentView)
            {
                _currentView = value;
                OnPropertyChanged("CurrentView");
            }
        }
    }

    public MainPageViewModel()
    {
        _viewModels = new List<ViewModelBase>();

        // Add view models here
        _viewModels.Add(new InputSelectViewModel());

        _viewIndex = 0;
        CurrentView = _viewModels[_viewIndex];
    }
}
class MainPageViewModel:ViewModelBase,INotifyPropertyChanged
{ 
私有ViewModelBase\u currentView;
私有列表视图模型;
私有int_视图索引;
公共视图模型库当前视图
{
获取{return\u currentView;}
设置
{
如果(值!=\u当前视图)
{
_当前视图=值;
OnPropertyChanged(“当前视图”);
}
}
}
公共MainPageViewModel()
{
_viewModels=新列表();
//在此处添加视图模型
_添加(新输入选择ViewModel());
_viewIndex=0;
CurrentView=_viewModels[_viewIndex];
}
}

问题似乎在于我的ViewModels不是公共的。我在输出中注意到以下类型的消息:

System.Windows.Data Error: Cannot get '<Whatever property of View Model>' value ...
System.Windows.Data错误:无法获取“”值。。。
我绑定到的每个属性都会出现此消息,包括
CurrentView
。将视图模型公开后,绑定工作得非常好。对于我来说,Silverlight在WPF中作为私有类正常工作时需要将类公开,这似乎有些奇怪,但这是我第一次使用Silverlight,所以这种复杂的情况让我大吃一惊。希望这个答案能对将来的人有所帮助。无论如何,这是绑定不起作用的主要原因,因此,
CurrentView
没有显示

对我来说调试这个问题很困难的另一个原因是我的页面意外地缓存了所有内容,所以我的一些更改不会显示出来。这个链接帮助了我:


如果它看起来有效,则应显示隐式数据模板。那么有什么不对呢?确保您使用Silverlight 5,并且您的浏览器使用SL5运行时,因为自SL5以来仅支持隐式数据模板。还有一件事你可以试着绕过这个问题:显式地设置模板,看看它是否显示出来。喜欢