Strange error in XAML: error in type converting through StaticResource



  • I didn't know how to briefly describe the problem.

    Here's the code xaml

    <Window x:Class="Test.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:Test"
            xmlns:loc="clr-namespace:LocalizerLib;assembly=LocalizerLib"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <loc:LocalizedString x:Key="str" OriginalString="qwe"></loc:LocalizedString>
        </Window.Resources>
        <Grid>
            <TextBlock Text="{StaticResource str}"/>
        </Grid>
    </Window>
    

    This is code c# class LocalizedString

    public class LocalizedString : IComparable, ICloneable, IEnumerable<char>, IEquatable<String>, IEquatable<LocalizedString>
    {
        public LocalizedString(string str)
        {
            OriginalString = str;
        }
    
    public LocalizedString()
    {
        OriginalString = "";
    }
    
    public string OriginalString { get; set; }
    
    public string LocalizeString
    {
        get
        {
            return InteractiveLocalizer.Localize(OriginalString);
        }
    }
    
    public override string ToString()
    {
        return LocalizeString;
    }
    
    public int CompareTo(object obj)
    {
        if (obj == null)
            return 1;
        var str = obj as string;
        if (str != null)
            return LocalizeString.CompareTo(str);
        var lstr = obj as LocalizedString;
        if (lstr != null)
            return LocalizeString.CompareTo(lstr.LocalizeString);
        throw new ArgumentException("LocalizedDtring: Bad type");
    }
    
    public object Clone()
    {
        return new LocalizedString(OriginalString);
    }
    
    public IEnumerator&lt;char&gt; GetEnumerator()
    {
        return LocalizeString.GetEnumerator();
    }
    
    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
    
    public bool Equals(string other)
    {
        return other == LocalizeString || other == OriginalString;
    }
    
    public bool Equals(LocalizedString other)
    {
        return OriginalString == other.OriginalString;
    }
    
    public static implicit operator LocalizedString(string v)
    {
        return new LocalizedString(v);
    }
    
    public static explicit operator String(LocalizedString ls)
    {
        return ls.LocalizeString;
    }
    

    }

    In this case, LocalizedString steals the same line as qwe. That's what I expected, but when the prophecy goes down with the exception. Here's the text.

    "qwe" is not an acceptable value for Text.

    How so?

    There is also an error during the compilation (whether it allows the annex to be started). Here's her text.

    I can't use the LocalizerLib.LocalizedString object to.
    The characteristic that requires the type of System.String.

    Emlict and explict operators are identified in the class. What else is missing so I can put my type instead of string?



  • XAML is inspiring the accusator(s) of type (although not visible). Multiple converters are used, for example, to transform the binding results. And even in this case, operators are not used, preference is given to an association. TypeConverter (who, by default, supports the transformation to the line and simply causes ToString) However, no envelope is made when resources are used. Your code leads to a call:

    object res = new LocalizedString("foo");
    fooTextBlock.Text = res;
    

    With obvious consequences.

    I can suggest several ways:

    1. In lieu of strange colts with resources, increased markings can be used MarkupExtension♪ Method ProvideValue may return the arbitrary type and even take into account the properties of which object the value is now assigned. It'll be brief and natural to look at XAML.

      <TextBlock Text="{loc:Localize foo}">
      
    2. To continue town with crutches, but to make XAML call the converters:

      <TextBlock Text="{Binding Source={StaticResource foo}}">
      
    3. In fact, the WPF has a localization facility, but who cares about it. ♪ ♪

    P. S. Cod didn't test, could've lied.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2