开发者

loading a silverlight control with a custom constructor

开发者 https://www.devze.com 2023-01-27 05:45 出处:网络
I have a silverlight page in which I am loading a control. This control has its own viewmodel which I pass in to the .xaml.cs file thru its constructor. However I get an error when compiling. This is

I have a silverlight page in which I am loading a control. This control has its own viewmodel which I pass in to the .xaml.cs file thru its constructor. However I get an error when compiling. This is the error:

{No matching constructor found on type 'MySite.Views.SearchFlyOutWin'}

My main page makes a reference to the 'SearchFlyOutWin' like this

xmlns:part="clr-namespace:MySite.Views;assembly=MyS开发者_JAVA百科ite"

In my mainpage.xaml I have tried to load the control like this

<part:SearchFlyOutWin x:Name="searchFlyOutWin" Visibility="{Binding Converter={StaticResource BooleanToVisibilityConverter}, Path=IsSearchVisible}" />

The constructor in my SearchFlyOutWin.xaml.cs is like this

    public SearchFlyOutWin(ISearchFlyoutViewModel viewmodel)
    {
        InitializeComponent();
        DataContext = viewmodel;
    }

I get the error described above in my Mainpage.xaml.cs when it calls the InitializeComponent(); method.

I think I probably need to direct the clr to call the correct constructor when loading the searchwin in this line here below

<part:SearchFlyOutWin x:Name="searchFlyOutWin" Visibility="{Binding Converter={StaticResource BooleanToVisibilityConverter}, Path=IsSearchVisible}" />

Any ideas on how to correct this? ...Thanks for your time.


.

I have a silverlight page in which I am loading a control. This control has its own viewmodel which I pass in to the .xaml.cs file thru its constructor. However I get an error when compiling. This is the error:

{No matching constructor found on type 'MySite.Views.SearchFlyOutWin'}

If your own control's constructor takes some parameter(s), then you cannot use this control in XAML. In XAML, every control must have a constructor with no parameter. That is why, it shows the error message {No matching constructor found on type 'MySite.Views.SearchFlyOutWin'}, since XAML parser searches a constructor with no parameter in your control called SearchFlyOutWin, and it found none!

One soution is that remove the parameter from constructor, and define your Model in the XAML as resource, then set the DataContext to it. Like this,

<Window.Resources>
        <local:SearchFlyoutViewModel x:Key="model"/>
</Window.Resources>

<part:SearchFlyOutWin DataContext="{StaticResource model}"/>

Hope, it solves your problem.

.


If you're committed to passing the viewmodel to the object in the constructor (which I don't think is a bad thing), the only way I've found to do this is to create the object in code and then add it to its parent panel programatically. Setting up bindings in code is also possible, though the syntax is more complex than the XAML syntax. The code might looks something like:

SearchFlyOutWin searchFlyOutWin = new SearchFlyOutWin(viewModel);
Binding b = new Binding("");
b.Source = IsSearchVisible;
b.Converter = new BooleanToVisibilityConverter();
searchFlyOutWin.SetBinding(SearchFlyOutWin.VisibilityProperty, b);
SearchFlyOutWinParentPanel.Children.Add(searchFlyOutWin);

Where SearchFlyOutWinParentPanel is some panel that can accept children. If there's an alternate way to do this in XAML, I'd love to see it, but I haven't found it yet.


You may need to set you viewmodel class to be public. Because I guess your viewmodel class will be in another namespace other than view.

0

精彩评论

暂无评论...
验证码 换一张
取 消