locked
Windows Class Library in MVVM : access ReadOnly properties from ViewModel. Use Behaviors ?

    Question

  • Hello, 

    i'm currently developping a Windows Class Library in c# / Xaml. This library will be integrated to an windows 8 ModernUI app. I use MVVM design pattern, and XAML files are embedded ressources in the library. I load those XAML resources at runtime using XamlReader. 

    var xamlPageStream = typeof(EntryPoint).GetTypeInfo().Assembly.GetManifestResourceStream(nameInPackage);
    var sr = new StreamReader(xamlPageStream);
    var xamlPage = sr.ReadToEnd();
     return XamlReader.Load(xamlPage) as FrameworkElement;

    I need to read / access, from viewModel, to ReadOnly properties of xaml component, declared in my pages. For example, i need to read ActualHeight / ActualWidht of a canvas element, in my viewmodel.  
    Thoses properties are not bindable to a viewmodel property, because they are ReadOnly. 

    My first question : do you know a way to access those properties (only for reading purposes) from viewmodel ? 

    My investigations was unsuccessful for the moment, but i read something about using a behavior, added to canvas compopent, in order to get and send the looked for property to viewmodel, at each change of this property. 

    I tried to create a customized behavior, and to reference it on my Canvas Control. 

    I added Behavior SDK in my VS projects (Class Library and caller program). I referenced it in my XAML page. Here is an extract from code : 

    <Page ...
    xmlns:Interactivity="clr-namespace:Microsoft.Xaml.Interactivity;assembly=Microsoft.Xaml.Interactivity" ...>
    
    [...]
    
    <ContentControl ...>
                <Interactivity:Interaction.Behaviors>
    
                </Interactivity:Interaction.Behaviors>
            </ContentControl>

     

    The content assist of VS works fine with this code, and the compilation is ok. Unfortunately, at runtime, when XamlReader reads the XAML, i get an ParseException : 

    ParseException : The attachable property 'Behaviors' was not found in type 'Interaction'.

    Did i forget something ? Or did i misunderstood something ? Why does the XamlReader fail to find Behavior, whereas content assist and compilation success ? 

    Is there an other way to proceed ?  

    Thank you by advance for your answers. 

    Tuesday, February 17, 2015 10:31 AM

All replies

  • Hi Bjorn,

    ParseException : The attachable property 'Behaviors' was not found in type 'Interaction'. looks like you do not have BehaviourSDK referenced in your project, would you like to check it again?

    For your first question: do you know a way to access those properties (only for reading purposes) from viewmodel ? 

    BehaviorSDK might be the correct direction and you could pass a parameter to the ViewModel if you need get some read-only properties from View.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 18, 2015 2:54 AM
    Moderator
  • Thanks for your reply. 

    I referenced BehaviorSdk by checking the box in my DLL Project > Add Reference > Windows 8.1 > Extensions : 

    And I did the same for the Exe caller Project. 
    And then i reference it in my Xaml page by the code given in first post. 

    Did i forget something ? 

    Thanks


    Wednesday, February 18, 2015 9:17 AM
  • As I know XamlReader might have some potential risk while loading the xaml, let's say if we write the code as a User Control, and then reference that user control in our project, could it better.

    Besides, I would like to know the full code of your reference, try to ref the Core of Behavior SDK to see if your project works.

    xmlns:Core="using:Microsoft.Xaml.Interactions.Core"

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, February 19, 2015 6:11 AM
    Moderator
  • I tried to add Core but without result, the exception is launched before parsing Core elements, i think. 

    I made a little project sample that represents the full case (Class Library + MVVM + Behavior). It's probably more easy to understand.  The exception is launched when loading EmbeddedView.xaml in LibraryTestContainer (which is called by CallerApp). 

    Here is the link : exemple

    Thanks

    Thursday, February 19, 2015 8:15 AM
  • Hi Bjorn,

    Thanks for your code, I was able to reproduce the issue, I will further figure it out, any update will reply here:)

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 23, 2015 1:11 AM
    Moderator
  • Hi Bjorn r

    Looks like the dll does not contain the required information, see the screenshot below:

    The app is trying to get Interaction.Behaviors from TestMvvmClassLibrary.dll but unfortunately they are included in the Microsoft.Xaml.Interactivity.dll and Interactions.dll.

    I tried to create a UserControl and it can be used in the XAML successfully because its data included in the TestMvvmClassLibrary.dll, please try ILMerge.exe http://www.microsoft.com/en-us/download/confirmation.aspx?id=17630 to merge dll into one so that we can find the correct information from the dll.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, March 02, 2015 2:48 AM
    Moderator
  • Hello and thank you for your answer. 

    I tried to use ILMerge on my real project to merge Interactions SDK dlls and my DLL, but it failed with "Unresolved assembly reference not allowed: Windows." 

    My research seems to indicate that ILMerge will not work fine for WinRT applications (http://stackoverflow.com/questions/15455727/ilmerge-error-windows-phone-8-unresolved-assembly-reference) 

    Even if it worked, I'm not sure that using this solution would be a good one, because i would have to "remerge" manually my DLL after each evolution and changes before delivering it, and it's a bit risky and tedious. 

    Also, thank you very much for your help and suggestion.

    Tuesday, March 03, 2015 1:59 PM
  • Hi Bjorn,

    Yes, I agree it could not be a good idea, but that's the key reason why your design not work.

    Let's say is it possible we do not use the Xaml file by XamlReader.Load, but instead we use a usercontrol, for instance we can do a similar thing like this blog:http://timheuer.com/blog/archive/2012/03/07/creating-custom-controls-for-metro-style-apps.aspx

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, March 05, 2015 6:51 AM
    Moderator