如何将这些C#代码更改为Silverlight代码? [英] How can I change these c# codes to silverlight codes?

查看:121
本文介绍了如何将这些C#代码更改为Silverlight代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string strText = string.Empty;
   float fontSize = 100f;
   string font = "华文琥珀";
   Image img = Image.FromFile("black.png");
   private void button1_Click(object sender, EventArgs e)
   {
       this.panel1.Refresh();
       strText = txtContent.Text;

       if (!float.TryParse(txtSize.Text, out fontSize))
       {
           MessageBox.Show("input the font size");
           return;
       }
       font = cmbFont.Text;
       Graphics g = this.panel1.CreateGraphics();
       TextureBrush brush = new TextureBrush(img, WrapMode.Tile);
       g.DrawString(strText, new Font(font, fontSize), brush, new PointF(-5, 5));
   }


图形g = this.panel1.CreateGraphics();
TextureBrush brush = new TextureBrush(img,WrapMode.Tile);
上面的这两个代码在Silverlight中不正确,如何更改它们?


Graphics g = this.panel1.CreateGraphics();
TextureBrush brush = new TextureBrush(img, WrapMode.Tile);
These two codes above is not right in silverlight,how to change these?

推荐答案

即使在System.Windows.Forms中使用System.Drawing,该代码也没有意义.查看已完成的操作:创建Graphics的实例,然后在面板中绘制一些内容.一旦此面板无效(例如,您最小化应用程序并在以后激活它,或只是在该面板顶部放置另一个窗口),则显示内容将消失.这是您想要实现的行为吗?

通常,您不应该创建Graphics的实例,而应该处理Paint的事件,或者更好.在某些自定义控件中重写方法OnPaint.请看我过去的答案:
在mdi子表单之间画线 [在面板上捕获图形 [


只需创建System.Windows.Controls.TextBlock的实例:
http ://msdn.microsoft.com/zh-cn/library/system.windows.controls.textblock%28v=vs.95%29.aspx [ http://msdn.microsoft.com/zh-cn/library/system.windows.controls.panel%28v = vs.95%29.aspx [
This code makes no sense even in System.Windows.Forms with System.Drawing. Look at what''s done: you create an instance of Graphics, and draw something in a panel. As soon as this panel is invalidated (for example, you minimize application and activate it later, or just put another window on top of this panel), the show content will disappear. Is that the behavior you want to achieve?

Normally, you should not create an instance of Graphics, you should handle the event of Paint or, better yet. override the method OnPaint in some custom control. Pleas see my past answers:
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^].

But it has nothing do to with Silverlight, where a normal way of making some graphics is vector: basically, you create some shapes and put them on some container UI element. Same thing would apply to WPF. The best you can do is learning the basics of Silverlight and just implementing what you want. The attempt to "translate" some code from Forms application is not very productive; and an attempt to "translate" such illiterate code is, respectively, counterproductive.



Just create an instance of System.Windows.Controls.TextBlock: http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock%28v=vs.95%29.aspx[^].

Put it on some panel, using appropriate class derived from System.Windows.Controls.Panel. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.controls.panel%28v=vs.95%29.aspx[^].

On some user input, assign appropriate value to the property System.Windows.Controls.TextBlock, change the value of other attributes, such as FontSize.

Something like that.

—SA


这篇关于如何将这些C#代码更改为Silverlight代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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