English 中文(简体)
棱晶和磁光工具包
原标题:Prism and MVVM Light Toolkit

I m trying to put Prism (just for CompositeUI) and MVVM Light Toolkit (for MVVM architecture =D) working together, but I m getting some troubles with the Light ViewModelLocator. When we use the locator into a "simple" WPF application, we can use an application resource on App.xaml, like this:

<Application.Resources>
    <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
</Application.Resources>

但是,当我们使用棱镜时,我们的商法属于模块项目,而且没有上诉程序,那么我试图将这一资源放在观点资源中:

<Window.Resources>
    <ResourceDictionary>
        <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
    </ResourceDictionary>
</Window.Resources>

这只是偏离了我的设计模式(也是时间的)错误,但《观点》却出现在分配给它的区域内。

Somebody already tried to do something like this? Is it possible to put Prism and MVVM Light working together?

这是我的“完整”法典:

(Light.xaml)

<Window x:Class="TryERP2.Financeiro.View.ViewLight"
    ... a lot of NS ...
    mc:Ignorable="d"
    xmlns:vm="clr-namespace:TryERP2.Financeiro.ViewModel"
    d:DesignHeight="150" d:DesignWidth="245" SizeToContent="WidthAndHeight">
    <Window.Resources>
        <ResourceDictionary>
            <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
        </ResourceDictionary>
    </Window.Resources>

    <Grid DataContext="{Binding Light, Source={StaticResource Locator}}">
        <Button Content="{Binding MyButtonText}" HorizontalAlignment="Stretch" Name="button1" VerticalAlignment="Stretch" />
    </Grid>
</Window>

ViewLightModel.cs:

public class ViewLightModel : ViewModelBase
{
    public ViewLightModel()
    { }

    public const string MyButtonTextPropertyName = "MyButtonText";
    private string _myButtonText = "Button Text";

    public string MyButtonText
    {
        get
        { return _myButtonText; }

        set
        {
            if (_myButtonText == value)
            { return; }

            _myButtonText = value;
            RaisePropertyChanged(MyButtonTextPropertyName);
        }
    }
}

Financeiro.cs (The module initializer class ... Partially shown ... Just where I register and "invoke" the views):

        var container = ServiceLocator.Current.GetInstance<IUnityContainer>();
        container.RegisterType<Object, ViewLight>("ViewLight");

        var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
        var main = new Uri("ViewLight", UriKind.Relative);
        regionManager.RequestNavigate("Desktop", main);

“热”甚微电离层电离层电离层电离层电离层电离层电离层电离层电离层。 该档案有这一结构:

<Application x:Class="TryERP2.Financeiro.App"
         ... a lot of NS ...
         xmlns:vm="clr-namespace:TryERP2.Financeiro.ViewModel"
         StartupUri="MainWindow.xaml"
         mc:Ignorable="d">

    <Application.Resources>
        <vm:ViewModelLocator x:Key="Locator"
                             d:IsDataSource="True" />
    </Application.Resources>

</Application>

That s what happen when I execute my application. The View that was on the module should be loaded into this blank space, showing its Button, but nothing happened:

http://imageshack.us/photo/my-images/818/capturarwy.png

当我试图改变这一看法并提出一个“简单观点”(而不是使用MVVERLight)时,我的工作是完美的,如下文所示:

http://imageshack.us/photo/my-images/513/capturar1a.png

最佳回答

I m sorry. I made a big mistake. As you may have noticed, I m building a Ribbon Application, and I m trying to show the controls under a blank Region. The items that can be displayed there are Controls. It wasn t working because I was trying to display a Window:

<Window x:Class="TryERP2.Financeiro.View.ViewLight"
... a lot of NS ...
mc:Ignorable="d"
xmlns:vm="clr-namespace:TryERP2.Financeiro.ViewModel"
d:DesignHeight="150" d:DesignWidth="245" SizeToContent="WidthAndHeight">
... etc

我通过将其改为用户控制,而不是一个窗口来确定,它的工作是完美的。

My new ViewLight.xaml:

<UserControl x:Class="TryERP2.Financeiro.View.ViewLight"
        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"
        xmlns:vm="clr-namespace:TryERP2.Financeiro.ViewModel"

        d:DesignHeight="150" d:DesignWidth="245">
    <UserControl.Resources>
        <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
    </UserControl.Resources>

    <Grid DataContext="{Binding Light, Source={StaticResource Locator}}">
        <Button Content="{Binding MyButtonText}" HorizontalAlignment="Stretch" Name="button1" VerticalAlignment="Stretch" />
    </Grid>
</UserControl>

我的新观点 (背后):

public partial class ViewLight : UserControl
{
    public ViewLight()
    {
        InitializeComponent();
    }
}

而观点仍然一样。

问题回答

观点。

<Window.Resources>
    <ResourceDictionary>
        <vm:ViewModelLoca纽约总部r x:Key="Loca纽约总部r" d:IsDataSource="True" />
    </ResourceDictionary>
</Window.Resources>

纽约总部

<Window.Resources>
        <vm:ViewModelLoca纽约总部r x:Key="Loca纽约总部r" d:IsDataSource="True" />
</Window.Resources>

Because Window.Resources is already a ResourceDictionary, and you create an another in it. You cannot access Loca纽约总部r resource here:

<Grid DataContext="{Binding Light, Source={StaticResource Loca纽约总部r}}">

Another option is 纽约总部 use MergedDictionary.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签