WPF常见问题的问与答[1]
2007-08-08 15:39
330 查看
1. 如何设置链接?
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<Border Background="LightGray" CornerRadius="40" Padding="10" Height="140" HorizontalAlignment="Center" VerticalAlignment="center" BorderBrush="Black" BorderThickness="1">
<TextBlock VerticalAlignment="center"><Hyperlink NavigateUri="http://www.zpxp.com/">BrawDraw.Com Online</Hyperlink></TextBlock>
</Border>
</Grid>
(1)注意这句:<Hyperlink NavigateUri="http://www.zpxp.com/">BrawDraw.Com Online</Hyperlink>
2. 如何画圆角矩形?
注意上面(1)Border标记中的CornerRadius="40",它用来指定圆角矩形的圆角半径
3. 怎样象HTML中的CSS一样设置样式?
使用Style - Setter方式:
<Style TargetType="{x:Type Rectangle}">
<Setter Property="Rectangle.RadiusX" Value="10"/>
<Setter Property="Rectangle.RadiusY" Value="10"/>
</Style>
TargetType目标类型,Setter进行对Property属性赋于Value指定的值.
[关于此问题,可以参见我的另一篇文章:<使用WPF创建炫亮按钮> http://blog.csdn.net/johnsuna/archive/2007/08/07/1729039.aspx 第3点:使用Application.Resources设置按钮属性(类似CSS样式单)]
4. 排列类似表格或单元格之类的使用什么?
使用Grid标记,比如排日历表
5. 如何画直线?
类似:<Rectangle Fill="Black" RadiusX="0" RadiusY="0" Height="1" Margin="0,20,0,0"/>
6. 如何使用底层API进行图形图像绘制而不是XAML?
首先,由于WPF中不象GDI+中有Graphics对象,因此你无法使用Graphics进行绘图了,取而代之的是:DrawingContext;类似的,GDI+中的OnPaint已被OnRender取代。
其次,UIElement有一个OnRendar方法,它的定义是:
protected virtual void OnRender (
DrawingContext drawingContext
)
但我们不能直接调用OnRender方法,也不能直接创建DrawingContext实例,但可以利用 DrawingGroup.Open 和DrawingVisual.RenderOpen。
这里举两个例子:(1)自定义绘制Canvas:(2)保存图片到文件。更多详情见这篇文章:“WPF中,如何使用图像API进行图形图像绘制而不是XAML? ”
7.如何在 WPF 中嵌入Flash(ActiveX): http://blogs.msdn.com/jijia/archive/2007/06/07/wpf-flash-activex.aspx
8. 在WPF下如何获取显示屏幕的高度和宽度,就像winform下的System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width(Height)一样?
double h = SystemParameters.PrimaryScreenHeight;
double w = SystemParameters.PrimaryScreenWidth;
以下是XAML代码,它将屏幕宽度绑定到Button的宽度:
<Button FontSize="8" Margin="10, 10, 5, 5" Grid.Column="0" Grid.Row="5"
HorizontalAlignment="Left"
Height="{x:Static SystemParameters.CaptionHeight}"
Width="{x:Static SystemParameters.PrimaryScreenWidth}">
SystemParameters
</Button>
9. 怎样制作一个带三角形的且有文字的自定义控件?就象下图一样:
很简单,你只需要将TextBlock,Polygon用<StackPanel>组合之后放入Button标签内即可!
<StackPanel>
<Button Height="50" Width="50">
<StackPanel>
<TextBlock>Play</TextBlock>
<Polygon Points="0,0 0,26 17,13" Fill="Black" />
</StackPanel>
</Button>
</StackPanel>
够简单的吧?
10. WPF内如何加载.net1.1或2.0的控件?
你可以这样加载.net1.1或2.0的控件到WPF的XAML中:
<Window x:Class="HostingWfInWpf.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="HostingWfInWpf"
>
<Grid>
<WindowsFormsHost>
<wf:MaskedTextBox x:Name="mtbDate" Mask="00/00/0000"/>
</WindowsFormsHost>
</Grid>
</Window>
同理,你也可以加载Forms中你的自定义控件:
<WindowsFormsHost>
<wf:BrawDrawGraphControl x:Name="brawdrawControl1" Width="500" Height="300" />
</WindowsFormsHost>
所不同的是,你需要在xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" 将clr-namespace和assembly设置为相应的值。
如果是C#代码呢?将上面的XAML转成相应的C#代码即可。别告诉我你不会哟,如果不会,你就需要加深WPF基础知识的学习。加油!
11. 如何防止WPF控件的遮盖而无法解发相关事件?
在WPF中,控件是可以树型嵌套的,例如:
<Grid Name ="mainGrid" >
<Canvas Name ="mainCanvas" Background="Green"/>
</Grid>
这时如果mainGrid容器里有个Button, 因为mainCanvas的遮盖而无法触发Click事件(即使mainCanvas背景为透明也不能触发)。此时,你需要将背景色设为null,这样就可以穿透mainCanvas容器的遮盖了。也有一个前提条件,那就是mainCanvas中没有控件遮盖mainGrid的Button。
12. WPF与GDI+中的颜色有区别吗?
你可以参见我的这篇文章。《GDI+与WPF中的颜色简析》http://blog.csdn.net/johnsuna/archive/2007/08/27/1761061.aspx
13. WPF中InkCanvas(墨水面板)用法?
参见周银辉的这篇文章http://www.cnblogs.com/zhouyinhui/archive/2007/08/08/841569.html,赞一下,写得不错!
14. 控制WPF图片的缩放绘图时的质量?
使用BitmapScalingMode枚举:
HighQuality:高质量的缩放,比Bilinear缩放方式要慢。
LowQuality:使用Bilinear缩放方式,输出的图像质量相对较低。
Unspecified:使用默认的缩放方式。
15. WINFORM中如何加入FLASH?又如何在WPF中插入FLASH?
(1)启动vs2005, 新建一个C#应用程序
(2)在工具箱里添加一个flash组件,flash.ocx,它是flash的容器,把它拖到Form的内容区,并将之改名为mainFlash
(3)双击Form窗体加入以下代码:
string swfFileName = @"test.swf";//这里你需要根据你的SWF位置进行调整
private void MainForm_Load(object sender, System.EventArgs e)
{
swfFileName = Application.StartupPath + swfFileName; //根据需要进行调整
mainFlash.LoadMovie(0, swfFileName);
}
如果你FLASH中有什么事件处理(比如点击按钮),那么你可以再找到mainFlash的事件FSCommand,加入下面的代码:
private void mainFlash_FSCommand(object sender, AxShockwaveFlashObjects._IShockwaveFlashEvents_FSCommandEvent e)
{
MessageBox.Show("Command:" + e.command.ToString() + "/r/n" + "Args:" + e.args .ToString());
}
按F5或Ctrl+F5运行程序,我们会发现FLASH已正常运行。如有上述之点击按钮,你可以点击flash上的按钮,第一行显示FLASH的命令,第二行显示参数。
如何在WPF中插入FLASH?
见这里: http://blogs.msdn.com/jijia/archive/2007/06/07/wpf-flash-activex.aspx
16. Creating Images from Raw Pixel Data in WPF
double dpi = 96;
int width = 128;
int height = 128; byte[] pixelData = new byte[width*height];
for (int y = 0; y < height; ++y)
{
int yIndex = y * width;
for (int x = 0; x < width; ++x)
{
pixelData[x + yIndex] = (byte) (x + y);
}
}
BitmapSource bmpSource = BitmapSource.Create(width, height, dpi, dpi,
PixelFormats.Gray8, null, pixelData, width);
This creates a
URL: http://www.interact-sw.co.uk/iangblog/2006/03/10/wpfrawpixels
17.Get Pixel from BitmapSource
private byte[] GetArrayOfPixels(BitmapSource bitmapsource)
{
Int32 stride = bitmapsource.PixelWidth * bitmapsource.Format.BitsPerPixel / 8;
Int32 ByteSize = stride * bitmapsource.PixelHeight * bitmapsource.Format.BitsPerPixel/8;
byte[] arrayofpixel = new byte[ByteSize];
bitmapsource.CopyPixels(arrayofpixel, stride, 0);
return arrayofpixel;
}
private Color ConvertPixelValue(byte[] pixelsvalue, int x, int y)
{
int NumOfBytes = format.BitsPerPixel / 8;
int StartPosition = (y * ImageWidth * (format.BitsPerPixel / 8)) + ( x * (format.BitsPerPixel / 8) );
byte colorvalue = pixelsvalue[StartPosition];
return System.Windows.Media.Color.FromRgb(colorvalue, colorvalue, colorvalue);
//int colorvalue = Convert.ToInt32(pixelsvalue[StartPosition]);
//return System.Drawing.Color.FromArgb(colorvalue,colorvalue,colorvalue);
}
调用:
System.IO.Stream StreamSource = new FileStream (“namafile.tiff”, FileMode.Open, FileAccess.Read, FileShare.Read);
System.Windows.Media.Imaging.TiffBitmapDecoder decoder = new TiffBitmapDecoder (StreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bitmapSource = decoder.Frames[0];
byte[] arrayofpixels = GetArrayOfPixels(bitmapSource);
Color c = ConvertPixelValue ( arrayofpixels, 0, 1 );
URL: http://geeks.netindonesia.net/blogs/de_joker/archive/2007/08/28/Get-Pixel-from-BitmapSource.aspx
18. How to convert between WPF BitmapSource objects and a LEAD RasterImage
http://support.leadtools.com/SupportPortal/cs/forums/15388/ShowPost.aspx
19. 使用RichTextBox的技巧:
见:WPF中RichTextBox的使用小窍门(翻译、整理)
20. 如何旋转一幅图片?(NEW!)
其实,超简单:
XAML代码:
<Image Width="150" Margin="5">
<Image.Source>
<BitmapImage UriSource="/images/myPhoto.jpg" Rotation="Rotate90" />
</Image.Source>
</Image>
C#代码:
Image rotated270 = new Image();
rotated270.Width = 150;
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(@"pack://application:,,/images/myPhoto.jpg");
bi.Rotation = Rotation.Rotate270;
bi.EndInit();
rotated270.Source = bi;
更多问题见:
WPF常见问题的问与答[2](不断更新中)
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<Border Background="LightGray" CornerRadius="40" Padding="10" Height="140" HorizontalAlignment="Center" VerticalAlignment="center" BorderBrush="Black" BorderThickness="1">
<TextBlock VerticalAlignment="center"><Hyperlink NavigateUri="http://www.zpxp.com/">BrawDraw.Com Online</Hyperlink></TextBlock>
</Border>
</Grid>
(1)注意这句:<Hyperlink NavigateUri="http://www.zpxp.com/">BrawDraw.Com Online</Hyperlink>
2. 如何画圆角矩形?
注意上面(1)Border标记中的CornerRadius="40",它用来指定圆角矩形的圆角半径
3. 怎样象HTML中的CSS一样设置样式?
使用Style - Setter方式:
<Style TargetType="{x:Type Rectangle}">
<Setter Property="Rectangle.RadiusX" Value="10"/>
<Setter Property="Rectangle.RadiusY" Value="10"/>
</Style>
TargetType目标类型,Setter进行对Property属性赋于Value指定的值.
[关于此问题,可以参见我的另一篇文章:<使用WPF创建炫亮按钮> http://blog.csdn.net/johnsuna/archive/2007/08/07/1729039.aspx 第3点:使用Application.Resources设置按钮属性(类似CSS样式单)]
4. 排列类似表格或单元格之类的使用什么?
使用Grid标记,比如排日历表
5. 如何画直线?
类似:<Rectangle Fill="Black" RadiusX="0" RadiusY="0" Height="1" Margin="0,20,0,0"/>
6. 如何使用底层API进行图形图像绘制而不是XAML?
首先,由于WPF中不象GDI+中有Graphics对象,因此你无法使用Graphics进行绘图了,取而代之的是:DrawingContext;类似的,GDI+中的OnPaint已被OnRender取代。
其次,UIElement有一个OnRendar方法,它的定义是:
protected virtual void OnRender (
DrawingContext drawingContext
)
但我们不能直接调用OnRender方法,也不能直接创建DrawingContext实例,但可以利用 DrawingGroup.Open 和DrawingVisual.RenderOpen。
这里举两个例子:(1)自定义绘制Canvas:(2)保存图片到文件。更多详情见这篇文章:“WPF中,如何使用图像API进行图形图像绘制而不是XAML? ”
7.如何在 WPF 中嵌入Flash(ActiveX): http://blogs.msdn.com/jijia/archive/2007/06/07/wpf-flash-activex.aspx
8. 在WPF下如何获取显示屏幕的高度和宽度,就像winform下的System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width(Height)一样?
double h = SystemParameters.PrimaryScreenHeight;
double w = SystemParameters.PrimaryScreenWidth;
以下是XAML代码,它将屏幕宽度绑定到Button的宽度:
<Button FontSize="8" Margin="10, 10, 5, 5" Grid.Column="0" Grid.Row="5"
HorizontalAlignment="Left"
Height="{x:Static SystemParameters.CaptionHeight}"
Width="{x:Static SystemParameters.PrimaryScreenWidth}">
SystemParameters
</Button>
9. 怎样制作一个带三角形的且有文字的自定义控件?就象下图一样:
很简单,你只需要将TextBlock,Polygon用<StackPanel>组合之后放入Button标签内即可!
<StackPanel>
<Button Height="50" Width="50">
<StackPanel>
<TextBlock>Play</TextBlock>
<Polygon Points="0,0 0,26 17,13" Fill="Black" />
</StackPanel>
</Button>
</StackPanel>
够简单的吧?
10. WPF内如何加载.net1.1或2.0的控件?
你可以这样加载.net1.1或2.0的控件到WPF的XAML中:
<Window x:Class="HostingWfInWpf.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="HostingWfInWpf"
>
<Grid>
<WindowsFormsHost>
<wf:MaskedTextBox x:Name="mtbDate" Mask="00/00/0000"/>
</WindowsFormsHost>
</Grid>
</Window>
同理,你也可以加载Forms中你的自定义控件:
<WindowsFormsHost>
<wf:BrawDrawGraphControl x:Name="brawdrawControl1" Width="500" Height="300" />
</WindowsFormsHost>
所不同的是,你需要在xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" 将clr-namespace和assembly设置为相应的值。
如果是C#代码呢?将上面的XAML转成相应的C#代码即可。别告诉我你不会哟,如果不会,你就需要加深WPF基础知识的学习。加油!
11. 如何防止WPF控件的遮盖而无法解发相关事件?
在WPF中,控件是可以树型嵌套的,例如:
<Grid Name ="mainGrid" >
<Canvas Name ="mainCanvas" Background="Green"/>
</Grid>
这时如果mainGrid容器里有个Button, 因为mainCanvas的遮盖而无法触发Click事件(即使mainCanvas背景为透明也不能触发)。此时,你需要将背景色设为null,这样就可以穿透mainCanvas容器的遮盖了。也有一个前提条件,那就是mainCanvas中没有控件遮盖mainGrid的Button。
12. WPF与GDI+中的颜色有区别吗?
你可以参见我的这篇文章。《GDI+与WPF中的颜色简析》http://blog.csdn.net/johnsuna/archive/2007/08/27/1761061.aspx
13. WPF中InkCanvas(墨水面板)用法?
参见周银辉的这篇文章http://www.cnblogs.com/zhouyinhui/archive/2007/08/08/841569.html,赞一下,写得不错!
14. 控制WPF图片的缩放绘图时的质量?
使用BitmapScalingMode枚举:
HighQuality:高质量的缩放,比Bilinear缩放方式要慢。
LowQuality:使用Bilinear缩放方式,输出的图像质量相对较低。
Unspecified:使用默认的缩放方式。
15. WINFORM中如何加入FLASH?又如何在WPF中插入FLASH?
(1)启动vs2005, 新建一个C#应用程序
(2)在工具箱里添加一个flash组件,flash.ocx,它是flash的容器,把它拖到Form的内容区,并将之改名为mainFlash
(3)双击Form窗体加入以下代码:
string swfFileName = @"test.swf";//这里你需要根据你的SWF位置进行调整
private void MainForm_Load(object sender, System.EventArgs e)
{
swfFileName = Application.StartupPath + swfFileName; //根据需要进行调整
mainFlash.LoadMovie(0, swfFileName);
}
如果你FLASH中有什么事件处理(比如点击按钮),那么你可以再找到mainFlash的事件FSCommand,加入下面的代码:
private void mainFlash_FSCommand(object sender, AxShockwaveFlashObjects._IShockwaveFlashEvents_FSCommandEvent e)
{
MessageBox.Show("Command:" + e.command.ToString() + "/r/n" + "Args:" + e.args .ToString());
}
按F5或Ctrl+F5运行程序,我们会发现FLASH已正常运行。如有上述之点击按钮,你可以点击flash上的按钮,第一行显示FLASH的命令,第二行显示参数。
如何在WPF中插入FLASH?
见这里: http://blogs.msdn.com/jijia/archive/2007/06/07/wpf-flash-activex.aspx
16. Creating Images from Raw Pixel Data in WPF
double dpi = 96;
int width = 128;
int height = 128; byte[] pixelData = new byte[width*height];
for (int y = 0; y < height; ++y)
{
int yIndex = y * width;
for (int x = 0; x < width; ++x)
{
pixelData[x + yIndex] = (byte) (x + y);
}
}
BitmapSource bmpSource = BitmapSource.Create(width, height, dpi, dpi,
PixelFormats.Gray8, null, pixelData, width);
This creates a
BitmapSourceof a linear gradient fill from black to white from top left to bottom right.
URL: http://www.interact-sw.co.uk/iangblog/2006/03/10/wpfrawpixels
17.Get Pixel from BitmapSource
private byte[] GetArrayOfPixels(BitmapSource bitmapsource)
{
Int32 stride = bitmapsource.PixelWidth * bitmapsource.Format.BitsPerPixel / 8;
Int32 ByteSize = stride * bitmapsource.PixelHeight * bitmapsource.Format.BitsPerPixel/8;
byte[] arrayofpixel = new byte[ByteSize];
bitmapsource.CopyPixels(arrayofpixel, stride, 0);
return arrayofpixel;
}
private Color ConvertPixelValue(byte[] pixelsvalue, int x, int y)
{
int NumOfBytes = format.BitsPerPixel / 8;
int StartPosition = (y * ImageWidth * (format.BitsPerPixel / 8)) + ( x * (format.BitsPerPixel / 8) );
byte colorvalue = pixelsvalue[StartPosition];
return System.Windows.Media.Color.FromRgb(colorvalue, colorvalue, colorvalue);
//int colorvalue = Convert.ToInt32(pixelsvalue[StartPosition]);
//return System.Drawing.Color.FromArgb(colorvalue,colorvalue,colorvalue);
}
调用:
System.IO.Stream StreamSource = new FileStream (“namafile.tiff”, FileMode.Open, FileAccess.Read, FileShare.Read);
System.Windows.Media.Imaging.TiffBitmapDecoder decoder = new TiffBitmapDecoder (StreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bitmapSource = decoder.Frames[0];
byte[] arrayofpixels = GetArrayOfPixels(bitmapSource);
Color c = ConvertPixelValue ( arrayofpixels, 0, 1 );
URL: http://geeks.netindonesia.net/blogs/de_joker/archive/2007/08/28/Get-Pixel-from-BitmapSource.aspx
18. How to convert between WPF BitmapSource objects and a LEAD RasterImage
http://support.leadtools.com/SupportPortal/cs/forums/15388/ShowPost.aspx
19. 使用RichTextBox的技巧:
见:WPF中RichTextBox的使用小窍门(翻译、整理)
20. 如何旋转一幅图片?(NEW!)
其实,超简单:
XAML代码:
<Image Width="150" Margin="5">
<Image.Source>
<BitmapImage UriSource="/images/myPhoto.jpg" Rotation="Rotate90" />
</Image.Source>
</Image>
C#代码:
Image rotated270 = new Image();
rotated270.Width = 150;
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(@"pack://application:,,/images/myPhoto.jpg");
bi.Rotation = Rotation.Rotate270;
bi.EndInit();
rotated270.Source = bi;
更多问题见:
WPF常见问题的问与答[2](不断更新中)
相关文章推荐
- WPF常见问题的问与答[1]
- 有关坐标系常见问题的问与答(转自ESRI中国社区)
- wpf开发常见问题(1)
- WPF 开发常见问题
- wpf开发常见问题(1)
- WPF代码注意事项,开发常见问题,知识总结
- WPF中当资源之间相互依赖(xaml资源与类对象资源相互依赖)时,常见无法加载的问题
- WPF:常见问题
- wpf数据绑定常见问题
- WPF代码注意事项,开发常见问题,知识总结
- WPF 常见性能或奇异问题总结。
- Mysql NDB 常见问题
- 局域网设置共享文件夹及常见问题解决办法
- c和指针 前三章知识点以及常见的问题和程序分析
- IE6.IE7,IE8,Firefox ,Safari,Chrome兼容性常见问题处理大集合
- ORACLE常见问题1000问(之六)
- VC调试常见问题分析
- ORACLE常见问题1000问(之十三)
- Proteus 常见问题集
- SERVER中一些常见性能问题的总结