在 XAML 中使用系统类型作为资源
我遇到过一种情况,直接在 XAML 中指定浮点值并将其用作我的多个 UI 片段的资源非常有用。经过搜索后,我发现了大量有关如何在 XAML 中包含正确的程序集 (mscorlib) 的信息,以便您可以做到这一点。
不幸的是,我在尝试执行此操作时遇到了一个例外。下面的 XAML 重现了这种情况:
<Window x:Class="davidtestapp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:core="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<core:Double x:Key="MyDouble">120</core:Double>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{StaticResource MyDouble}" />
<ColumnDefinition Width="40" />
<ColumnDefinition Width="40" />
</Grid.ColumnDefinitions>
<Rectangle Grid.Column="0" Fill="Red" />
<Rectangle Grid.Column="1" Fill="Green" />
<Rectangle Grid.Column="2" Fill="Blue" />
</Grid>
</Window>
当我尝试编译并运行它时,我收到一个向我抛出的 XamlParseException,它表示“'120' 不是属性 'Width' 的有效值”。
但是“Width”属性是一个双精度值,那么为什么我不能使用定义的 StaticResource 来设置它呢?有谁知道该怎么做?
I have encountered a situation where it would be very useful to specify a floating point value directly in XAML and use it as a resource for several of my UI pieces. After searching around I found a good amount of information on how to include the proper assembly (mscorlib) in your XAML so you can do just that.
Unfortunately, I am getting an exception in one instance where I try to do this. Here is the following XAML that recreates the situation:
<Window x:Class="davidtestapp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:core="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<core:Double x:Key="MyDouble">120</core:Double>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{StaticResource MyDouble}" />
<ColumnDefinition Width="40" />
<ColumnDefinition Width="40" />
</Grid.ColumnDefinitions>
<Rectangle Grid.Column="0" Fill="Red" />
<Rectangle Grid.Column="1" Fill="Green" />
<Rectangle Grid.Column="2" Fill="Blue" />
</Grid>
</Window>
When I attempt to compile and run this, I get an XamlParseException thrown at me which says that "'120' is not a valid value for property 'Width'".
But the "Width" property is a double, so why can't I set it using the StaticResource which was defined? Does anyone know how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不。ColumnDefinition.Width 的类型为 GridLength,这就是您收到错误的原因。如果你执行类似下面代码的操作,它应该可以正常工作。
No. ColumnDefinition.Width is of Type GridLength which is why you're getting the error. If you do something like the code below, it should work fine.
您遇到的问题是,在 ColumnDefinition 对象上,Width 属性不是双精度值,它是 GridLength 结构。如果您查看 ColumnDefinition 的 MSDN 文档。 Width 你会发现你不能将双精度值分配给 ColumnDefinition.Width
The problem you are encountering is that on the ColumnDefinition object, the Width property is NOT a double, it is a GridLength structure. If you look at the MSDN documentation for ColumnDefinition.Width you'll see that you cannot assign a double to a ColumnDefinition.Width