在 XAML 中使用系统类型作为资源

发布于 2024-09-18 15:28:49 字数 1181 浏览 0 评论 0原文

我遇到过一种情况,直接在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与他有关 2024-09-25 15:28:49

不。ColumnDefinition.Width 的类型为 GridLength,这就是您收到错误的原因。如果你执行类似下面代码的操作,它应该可以正常工作。

<Window.Resources>
    <core:Double x:Key="MyDouble">300</core:Double>
    <GridLength x:Key="MyGridLength">20</GridLength>
</Window.Resources>

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="{StaticResource MyGridLength}" />
        <ColumnDefinition Width="40" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>

    <Rectangle Grid.Column="0" Fill="Red" />
    <Rectangle Grid.Column="1" Fill="Green" />
    <Rectangle Grid.Column="2" Fill="Blue"  Width="{StaticResource MyDouble}"/>

</Grid>

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.

<Window.Resources>
    <core:Double x:Key="MyDouble">300</core:Double>
    <GridLength x:Key="MyGridLength">20</GridLength>
</Window.Resources>

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="{StaticResource MyGridLength}" />
        <ColumnDefinition Width="40" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>

    <Rectangle Grid.Column="0" Fill="Red" />
    <Rectangle Grid.Column="1" Fill="Green" />
    <Rectangle Grid.Column="2" Fill="Blue"  Width="{StaticResource MyDouble}"/>

</Grid>
最美不过初阳 2024-09-25 15:28:49

您遇到的问题是,在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文