简体   繁体   English

将代码移植到Silverlight时出错

[英]Errors while porting code to silverlight

I have a C# class library project in my solution, in which I added a User Control and pasted the following code from my WPF application. 我的解决方案中有一个C#类库项目,在其中添加了一个用户控件,并从WPF应用程序中粘贴了以下代码。

The xaml looks all fine, but I am getting certain errors on compiling. xaml看起来还不错,但是编译时出现了一些错误。 For eg: 例如:

1) System.Windows.DataFormats' does not contain a definition for 'StringFormat'
2) System.Windows.Controls.TextBlock' does not contain a definition for 'Background' and no extension method 'Background' accepting a first argument of type 'System.Windows.Controls.TextBlock' could be found
3) System.Windows.Media.Colors' does not contain a definition for 'LightCoral'
4) System.Windows.Controls.TextBlock' does not contain a definition for 'Background' and no extension method 'Background' accepting a first argument of type 'System.Windows.Controls.TextBlock' could be found (are you missing a using directive or an assembly reference?)

I am not sure why these errors are occuring and these are related to TextBox's background, Drag Drop, DataFormats etc. The .xaml.cs code is here as well. 我不确定为什么会发生这些错误,并且这些错误与TextBox的背景,拖放,DataFormats等有关。.xaml.cs代码也位于此处。 http://pastebin.com/CY8E8prz http://pastebin.com/CY8E8prz

You have to remove the properties referred to by the errors, because Silverlight simply doesn't have them or they have a different name (WPF and Silverlight aren't exactly the same, the former has more features than the latter). 您必须删除错误所引用的属性,因为Silverlight根本没有它们或它们具有不同的名称(WPF和Silverlight并不完全相同,前者比后者具有更多功能)。

A few examples: 一些例子:

  • To color the background of a TextBlock , you can wrap it in a Border (adjusting positioning/sizing attributes accordingly). 要为TextBlock的背景着色,您可以将其包装在Border (相应地调整定位/大小属性)。
  • LightCoral looks like a color name, you'll have to look up something similar or use Color.FromArgb() LightCoral看起来像一个颜色名称,您必须查找类似的内容或使用Color.FromArgb()

In short, copy-pasted code from WPF to Silverlight very rarely works as-is. 简而言之,从WPF复制粘贴到Silverlight的代码很少能保持原样。

Textblock has no background in Silverlight, only WPF. Textblock在Silverlight中没有背景,只有WPF。 I'm sure the rest of your errors follow suit. 我相信您其余的错误也会照做。 For that particular issue, place the textblock over another control or use a label instead. 对于该特定问题,请将文本块放置在另一个控件上或使用标签代替。 For the color, just look up the RGB and set it directly. 对于颜色,只需查找RGB并直接设置即可。 I'm not sure on the other errors exactly. 我不确定其他错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM