简体   繁体   中英

WPF my program has "MarkupExtension is not valid for Setter.Value" error but working fine in runtime

This error only appear when I added <ImageSource x:key="..."> image resource file </ImageSource> into my <application.resources> section. However the program working fine in runtime without any problem.

Before (original code => 0 error):

    <Application.Resources>
        <FontFamily x:Key="NotoBold">
            fonts/NotoSerifCJKtc-Bold.otf#Noto Serif CJK TC Bold
        </FontFamily>

        <Style x:Key="HeaderTitle" TargetType="TextBlock">
            <Setter Property="Grid.Column" Value="0" />
            <Setter Property="HorizontalAlignment" Value="Left" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="FontSize" Value="18" />
            <Setter Property="FontFamily" Value="{StaticResource NotoBold}" />
            <Setter Property="Foreground" Value="White" />
        </Style>
    ... ...
    ... ...
    ... ...
    </Application.Resources>

After (added ImageSource xkey='RedCircle1' => 1 error):

    <Application.Resources>
        <FontFamily x:Key="NotoBold">
            fonts/NotoSerifCJKtc-Bold.otf#Noto Serif CJK TC Bold
        </FontFamily>

        <ImageSource x:Key="RedCircle1">
            images/sample/circleRed1.png
        </ImageSource>

        <Style x:Key="HeaderTitle" TargetType="TextBlock">
            <Setter Property="Grid.Column" Value="0" />
            <Setter Property="HorizontalAlignment" Value="Left" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="FontSize" Value="18" />
            <Setter Property="FontFamily" Value="{StaticResource NotoBold}" />
            <Setter Property="Foreground" Value="White" />
        </Style>
    ... ...
    ... ...
    ... ...
    </Application.Resources>

The error hint appear on <Style x:Key="HeaderTitle"... > . If I change the setter value="{StaticResource NotoBold}" to something else(eg value="Verdana") the error will gone.

The error message: MarkupExtension is not valid for Setter.Value. The only supported MarkupExtension types are DynamicResourceExtension and BindingBase or derived types.

UPDATE

My.Net version is 4.8.03752, Visual Studio 2019 version is 16.8.3. All my resource files(Noto font and circleRed1.png) their build actions are "Resources".

Step to re-produce the problem.

  • create a new WPF .NET project.
  • include the resources(font & image) into project and build them as "Resources".
  • copy the source code into <application.resources> section. (Until this step everything still fine no any error message appear.)
  • Press "Run" to execute your program, a blank window will appear(we still haven't code xaml), then press "Stop" to exit the debugging/runtime.
  • Now the error message appear.
  • copy the source code into xaml page and run the program again.
  • Although there are some errors but the program working fine in runtime and all the resources(font & image) are display correctly.
  • screenshot of the error
  • program runtime & error on xaml page

My temporary solution:

  1. Remove one character of my StaticResource variable name, "NotoBold" => "otoBold"
  2. Now the error message become Can't recognized "otoBold"...
  3. Undo the edit, "otoBold" => "NotoBold"
  4. All error message gone. The XAML designer also display the Noto font correctly.

Now everytime after the debugging, I have to fix everything again, for else my xaml designer view won't display correctly(some controls their style attribute won't take effect).

Please try to modify the code like the following:

<Setter Property="FontFamily" Value="{DynamicResource NotoBold}" />

A StaticResource will be resolved and assigned to the property during the loading of the XAML which occurs before the application is actually run. It will only be assigned once and any changes to resource dictionary ignored.

A DynamicResource assigns an Expression object to the property during loading but does not actually lookup the resource until runtime when the Expression object is asked for the value. This defers looking up the resource until it is needed at runtime. You could analyze whether to use DynamicResource or StaticResource according to the specific situation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM