XAML 为什么Visual Studio显示错误消息"Windows. Resources is not found in the type Window(在类型窗口中找不到资源)"?

x6yk4ghg  于 2022-12-07  发布在  Windows
关注(0)|答案(1)|浏览(376)

我是WPF应用程序的初学者。我创建了一个WPF应用程序,并希望在应用程序中使用Main Page。在Window作用域中,我希望声明Window.Resources,但它返回两个错误:
1-无法识别或访问成员Resources
2-在类型Window中找不到可附加属性Resources
在这种情况下,可能会遗漏什么?
下面是我的XAML代码:

<Window x:Class="ArmsPosition.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
        xmlns:local="clr-namespace:ArmsPosition"
        mc:Ignorable="d"
        Loaded="Window_Loaded"
        Closing="Window_Closing"        
        WindowState="Normal"
        Title="Arms Positions" Height="560" Width="800" MinHeight="560" MinWidth="800" MaxHeight="560" MaxWidth="800">
    <Grid>
        <Window.Resources>
            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="FontFamily" Value="Segoe UI"/>
                <Setter Property="FontSize" Value="20"/>
                <Setter Property="Foreground" Value="#FF999999"/>
            </Style>
        </Window.Resources>

    </Grid>
</Window>
v440hwme

v440hwme1#

这是因为您在Grid元素中使用了Windows.ResourceResourceWindow元素的直接子属性。

<Window x:Class="ArmsPosition.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
        xmlns:local="clr-namespace:ArmsPosition"
        mc:Ignorable="d"
        Loaded="Window_Loaded"
        Closing="Window_Closing"        
        WindowState="Normal"
        Title="Arms Positions" Height="560" Width="800" MinHeight="560" MinWidth="800" MaxHeight="560" MaxWidth="800">
    <Window.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="FontFamily" Value="Segoe UI"/>
            <Setter Property="FontSize" Value="20"/>
            <Setter Property="Foreground" Value="#FF999999"/>
        </Style>
    </Window.Resources>
    <Grid>
        <!--SOME CONTROLS HERE-->
    </Grid>
</Window>

相关问题