简体   繁体   English

无法分配给属性“Windows.UI.Xaml.Controls.ContentControl.Content”

[英]Failed to assign to property 'Windows.UI.Xaml.Controls.ContentControl.Content'

In my windows 10 app I want to set button content from a resource file.在我的 Windows 10 应用程序中,我想从资源文件中设置按钮内容。 In the designer the vector image from the resource file is visible.在设计器中,资源文件中的矢量图像是可见的。 But in runtime the following exception occurs: An exception of type 'Windows.UI.Xaml.Markup.XamlParseException' occurred in Demo.UI.W10.exe but was not handled in user code WinRT information: Failed to assign to property 'Windows.UI.Xaml.Controls.ContentControl.Content'.但在运行时出现以下异常: Demo.UI.W10.exe 中发生了“Windows.UI.Xaml.Markup.XamlParseException”类型的异常,但未在用户代码 WinRT 信息中处理:无法分配给属性“Windows. UI.Xaml.Controls.ContentControl.Content'。 [Line: 15 Position: 17] Additional information: The text associated with this error code could not be found. [行:15 位置:17] 附加信息:找不到与此错误代码相关的文本。

This is my resourche dictionary Icons.xaml wich holds the vector image in the Viewbox control with the key WiFi:这是我的资源字典 Icons.xaml,其中包含带有关键 WiFi 的 Viewbox 控件中的矢量图像:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Viewbox x:Key="Wifi">
    <Grid Width="48" Height="48" Visibility="Visible">
        <Path Data="M16.104004,15.776998C17.199005,15.776998 18.086014,16.664998 18.086014,17.759999 18.086014,18.855977 17.199005,19.742999 16.104004,19.742999 15.009003,19.742999 14.12001,18.855977 14.12001,17.759999 14.12001,16.664998 15.009003,15.776998 16.104004,15.776998z M16.104004,10.558985C19.327011,10.558985,22.057007,12.679008,22.975006,15.601004L21.118011,16.756978C20.652008,14.412986 18.584015,12.646995 16.104004,12.646995 13.580002,12.646995 11.486008,14.474997 11.067001,16.87798L9.1930084,15.730001C10.070007,12.741997,12.831009,10.558985,16.104004,10.558985z M16.028015,5.2879915C21.153015,5.2879915,25.555008,8.378993,27.476013,12.796989L25.771011,13.859C24.221008,9.9980106 20.443008,7.2719988 16.028015,7.2719988 11.586014,7.2719988 7.7890015,10.031 6.2570038,13.929984L4.5440063,12.879997C6.4450073,8.4169874,10.871002,5.2879915,16.028015,5.2879915z M16.028015,0C23.047012,5.5224518E-08,29.114014,4.0700049,32,9.9789981L30.128006,11.144982C27.639008,5.8550076 22.262009,2.1920154 16.028015,2.1920151 9.7550049,2.1920154 4.3480072,5.9020047 1.881012,11.24801L0,10.094995C2.8630066,4.1239905,8.9640045,5.5224518E-08,16.028015,0z" Stretch="Uniform" Fill="#FF000000" Width="48" Height="48" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5">                
        </Path>
    </Grid>
</Viewbox>

This is my App.xaml where I add the Icons.xaml resource file in the MergedDictionaries:这是我的 App.xaml,我在 MergedDictionaries 中添加 Icons.xaml 资源文件:

<Application
x:Class="Demo.UI.W10.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Demo.UI.W10"
RequestedTheme="Light">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Assets/Icons.xaml"/>
            <ResourceDictionary Source="Assets/Styles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

In my mvvmcross page I use the vector image WiFi from the resource file to set the button content.在我的 mvvmcross 页面中,我使用资源文件中的矢量图像 WiFi 来设置按钮内容。

<views:MvxWindowsPage
x:Class="Demo.UI.W10.Views.ConnectionView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Demo.UI.W10.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:Demo.Core.ViewModels"
xmlns:views="using:Cirrious.MvvmCross.WindowsUWP.Views"
mc:Ignorable="d">
<Page.Resources>
    <vm:ConnectionViewModel x:Key="ViewModel"/>
</Page.Resources>
<Grid>
    <Button Content="{StaticResource Wifi}" Command="{Binding DemoCommand, Source={StaticResource ViewModel}}" Style="{StaticResource BtnMenuStyle}"/>
</Grid>

How can I use the vector image from the resource dictionary as button content?如何使用资源字典中的矢量图像作为按钮内容?

You can do placing the resources inside a DataTemplate:您可以将资源放置在 DataTemplate 中:

 <DataTemplate x:Key="Wifi">
        <Viewbox >
            <Grid Width="48" Height="48" Visibility="Visible">
                <Path Data="M16.104004,15.776998C17.199005,15.776998 18.086014,16.664998 18.086014,17.759999 18.086014,18.855977 17.199005,19.742999 16.104004,19.742999 15.009003,19.742999 14.12001,18.855977 14.12001,17.759999 14.12001,16.664998 15.009003,15.776998 16.104004,15.776998z M16.104004,10.558985C19.327011,10.558985,22.057007,12.679008,22.975006,15.601004L21.118011,16.756978C20.652008,14.412986 18.584015,12.646995 16.104004,12.646995 13.580002,12.646995 11.486008,14.474997 11.067001,16.87798L9.1930084,15.730001C10.070007,12.741997,12.831009,10.558985,16.104004,10.558985z M16.028015,5.2879915C21.153015,5.2879915,25.555008,8.378993,27.476013,12.796989L25.771011,13.859C24.221008,9.9980106 20.443008,7.2719988 16.028015,7.2719988 11.586014,7.2719988 7.7890015,10.031 6.2570038,13.929984L4.5440063,12.879997C6.4450073,8.4169874,10.871002,5.2879915,16.028015,5.2879915z M16.028015,0C23.047012,5.5224518E-08,29.114014,4.0700049,32,9.9789981L30.128006,11.144982C27.639008,5.8550076 22.262009,2.1920154 16.028015,2.1920151 9.7550049,2.1920154 4.3480072,5.9020047 1.881012,11.24801L0,10.094995C2.8630066,4.1239905,8.9640045,5.5224518E-08,16.028015,0z" Stretch="Uniform" Fill="#FF000000" Width="48" Height="48" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5">
                </Path>
            </Grid>
        </Viewbox>
    </DataTemplate>

And now simply现在简单地

<Button ContentTemplate="{StaticResource Wifi}"/>

You can use it as many times you need.您可以根据需要多次使用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法分配给属性“ Windows.UI.Xaml.Controls.ContentPresenter.Content” - Failed to assign to property 'Windows.UI.Xaml.Controls.ContentPresenter.Content' 无法分配给属性“ Windows.UI.Xaml.FrameworkElement.MinHeight” - Failed to assign to property 'Windows.UI.Xaml.FrameworkElement.MinHeight' C#XAML - 获取XAML内容属性(contentControl.Content) - C# XAML - Getting the XAML Content property (contentControl.Content) UWP-从不同样式的资源字典中引用StaticResource:无法分配给属性“ Windows.UI.Xaml.ResourceDictionary.Source” - UWP - Reference StaticResource from different style resource dictionary: Failed to assign to property 'Windows.UI.Xaml.ResourceDictionary.Source' 无法分配给属性“System.Windows.Controls.Primitives.ButtonBase.Click” - Failed to assign to property 'System.Windows.Controls.Primitives.ButtonBase.Click' 具有围绕内容的控件的ContentControl - ContentControl with controls around the content Windows.UI.Xaml.Controls.SearchBox在哪里? - Where is Windows.UI.Xaml.Controls.SearchBox? 无法从文本&#39;鼠标&#39;创建&#39;Windows.UI.Xaml.Controls.Primitives.PlacementMode&#39; - Failed to create a 'Windows.UI.Xaml.Controls.Primitives.PlacementMode' from the text '鼠标' 无法分配给属性“ Microsoft.Toolkit.Uwp.UI.Controls.AdaptiveGridView.DesiredWidth” - Failed to assign to property 'Microsoft.Toolkit.Uwp.UI.Controls.AdaptiveGridView.DesiredWidth' 命名空间 Windows.UI.Xaml.Controls 和 Microsoft.UI.Xaml.Controls 之间的区别 - Difference between namespace Windows.UI.Xaml.Controls and Microsoft.UI.Xaml.Controls
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM