Anonymous

My feedback

  1. 299 votes
    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      Signed in as (Sign out)

      We’ll send you updates on this idea

      Anonymous supported this idea  · 
      Anonymous commented  · 

      There is old lack of native XAML
      https://wpdev.uservoice.com/forums/110705-universal-windows-platform/suggestions/7232264-add-markup-extensions-to-and-improve-winrt-xaml

      I had seen post about custom markup extensions on UWP apps but it is not work at all
      https://msdn.microsoft.com/en-us/windows/uwp/xaml-platform/customresource-markup-extension
      At first, I install latest version of Visual Studio and try to create custom extension for localization but described in your article way with derive from CustomXamlResourceLoader class doesn't work :(
      I still have compiler errors:
      Cannot assign 'Localizing' into property 'Text', type must be assignable to 'String'
      Type 'local:Localizing' is used like a markup extension but does not derive from MarkupExtension.

      Second, please, see ABindingExtension class and read the article about it
      http://makeloft.xyz/lessons/xaml/custom-binding-extensions

      public abstract class ABindingExtension : Binding, IValueConverter
      {
      protected ABindingExtension()
      {
      Source = Converter = this;
      }

      protected ABindingExtension(object source) // Source, RelativeSource, null for DataContext
      {
      var relativeSource = source as RelativeSource;
      if (relativeSource == null && source != null) Source = source;
      else RelativeSource = relativeSource;
      Converter = this;
      }

      public abstract object Convert(object value, Type targetType, object parameter, CultureInfo culture);

      public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
      {
      throw new NotImplementedException();
      }
      }

      This class very good and memory safe alternative for classical MarkupExtension. This approach works even on old Windows Phone Silverlight where is not MarkupExtension class.

      I tried to apply this way to UWP but there are two artificial limitations of XAML-parser.

      Look at sample

      public abstract class ABindingExtension : Binding, IValueConverter
      {
      protected ABindingExtension()
      {
      Source = Converter = this;
      }

      protected ABindingExtension(object source) // Source, RelativeSource, null for DataContext
      {
      var relativeSource = source as RelativeSource;
      if (relativeSource == null && source != null) Source = source;
      else RelativeSource = relativeSource;
      Converter = this;
      }

      public abstract object Convert(object value, Type targetType, object parameter, string culture);

      public virtual object ConvertBack(object value, Type targetType, object parameter, string culture)
      {
      throw new NotImplementedException();
      }
      }

      public class Localizing : ABindingExtension
      {
      public override object Convert(object value, Type targetType, object parameter, string culture)
      {
      return "test"; // return value by key
      }
      }

      For example
      <Control>
      <Control.DataContext> // is object
      <Localizing/> // called Convert method like expected!
      <Control.DataContext>
      </Control>

      output => Works fine!
      But

      <TextBlock>
      <TextBlock.Text> // is string
      <Lokalizing/>
      <TextBlock.Text>
      </TextBlock>

      output => Cannot assign 'Localizing' into property 'Text', type must be assignable to 'String'

      Also
      <Control DataContext="{local:Localizing}"/>

      output => Type 'local:Localizing' is used like a markup extension but does not derive from MarkupExtension.

      Pease, check this artificial limitaions for Bindings becouse all works fine, but only XAML-preprocessor get errors!

      P.S. May be you will be interested by SwichConverter and some other power improvements for XAML and MVVM

      <Grid.Resources>
      <SwitchConverter x:Key="NumberSwitchConverter" Default="Hello">
      <Case Key="0" Value="Zero"/>
      <Case Key="1" Value="One"/>
      </SwitchConverter>
      </Grid.Resources>
      <TextBlock Text="{Binding Number, Converter={StaticResource NumberSwitchConverter}}"/>

      Number==0 => out: Zero
      Number==1 => out: One
      Number==2 => out: Hello

      Please, see code samples and Aero Framework library
      https://onedrive.live.com/?authkey=%21AMT2QVotfWTReos&cid=C8D6E2F8053DDCFB&id=C8D6E2F8053DDCFB%2123902&parId=C8D6E2F8053DDCFB%2123896&action=locate

      Thanks!

    Feedback and Knowledge Base