MVVM中的Windows类库:从ViewModel访问ReadOnly属性。使用行为? [英] Windows Class Library in MVVM : access ReadOnly properties from ViewModel. Use Behaviors ?

查看:58
本文介绍了MVVM中的Windows类库:从ViewModel访问ReadOnly属性。使用行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


你好, 





我目前正在开发c#/ Xaml中的Windows类库。该库将集成到Windows 8 ModernUI应用程序中。我使用MVVM设计模式,而XAML文件是库中的嵌入式资源。我在运行时使用XamlReader加载这些XAML资源。 

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







我需要从viewModel读取/访问我的页面中声明的xaml组件的ReadOnly属性。例如,我需要读取canvas元素的ActualHeight / ActualWidht,in我的viewmodel。 
$
Thoses属性不能绑定到viewmodel属性,因为它们是ReadOnly。 





我的第一个问题:您是否知道从viewmodel访问这些属性的方法(仅用于阅读目的)? 



我的调查暂时没有成功,但我读了一些关于使用行为的内容,添加到canvas compopent,以获取并将查找属性发送到viewmodel,每次更改这个属性。 





我尝试创建自定义行为,并在我的Canvas Control上引用它。 



我在我的VS项目中添加了Behavior SDK(类库和调用者程序)。我在我的XAML页面中引用了它。这是代码的摘录: 

 < Page   ... 
xmlns:Interactivity = " clr-namespace:Microsoft.Xaml.Interactivity; assembly = Microsoft.Xaml.Interactivity" ... >

[...]

< ContentControl ... >
< Interactivity:Interaction.Behaviors>

< / Interactivity:Interaction.Behaviors>
< / ContentControl>

 


VS的内容辅助工作正常,编译正常。不幸的是,在运行时,
当XamlReader读取XAML时,我得到一个ParseException: 



ParseException:在"交互"类型中找不到可附加属性"行为"。







我忘记了什么?或者我误解了什么?为什么XamlReader无法找到行为,而内容辅助和编译成功? 


< p style ="border:none; font-family:'Segoe UI','Lucida Grande',Verdana,Arial,Helvetica,sans-serif;概要:0像素;填充右:0像素;颜色:#333333;字体大小:14px的; line-height:20.1620178222656px">
还有其他方法可以继续吗? 









先谢谢您的回答。 

解决方案

嗨Bjorn,


ParseException:在"交互"类型中找不到可附加属性"行为"。看起来你没有在你的行为中引用BehaviourSDK项目,你想再检查一下吗?


第一个问题:你知道吗?这些属性(仅用于阅读目的)来自viewmodel? 


BehaviorSDK可能是正确的方向,如果需要阅读,可以将参数传递给ViewModel仅来自View的属性。


- James


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. 

解决方案

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


这篇关于MVVM中的Windows类库:从ViewModel访问ReadOnly属性。使用行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆