Get all WinRT application from regular Desktop application. Part 1.

Today I’ve been solving interesting task. All my investigation I’ll write on this article. A problem is following: there is a regular C# application which should display all installed WinRT application per user.

There is a whole section of all WinRT APIs for Windows 8 that are able to be called from a regular C# application on MSDN. This one includes a PackageManager class which allows getting all installed packages.

Access WinRT API from Console application.

First step, create a usual Console application.

Then we need reference a Window.dll. However, in the desktop projects Windows tab doesn’t appear by default. For this we need manually add following lines to csproj file.

After that step a new tab will appear:

Now we can try to get all WinRT installed packages. However, when we build that we will have following error:

The type ‘System.Collections.Generic.IEnumerable`1<T0>’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’.

Unfortunately this assembly doesn’t present on reference tab. For this you need add this assembly from C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5 folder.

Get all installed WinRT application per user.

We will get installed Metro/Modern packages per user because on same computer different users can install different applications. All this information is available by PackageManager class.

var packageManager = new PackageManager();
var metroApps = packageManager.FindPackagesForUser(WindowsIdentity.GetCurrent().User.Value);

That’s all and really simple, isn’t it? 😉

Hide inactive cursor behavior.

In this article I’ll explain one way how to hide a cursor after n-seconds of inactivity though WPF behavior mechanism. Same effect you can observe on almost any video player — if you don’t touch a mouse for the few seconds, cursor will disappear.

First of all, what is a behavior?

A behavior encapsulates pieces of functionality into a reusable component, which we later on can attach to an element in a view. Emphasis is on reusable. Behaviors also have the benefit of keeping the MVVM pattern intact, since we can move code from code behind to behaviors. You can find a good introduction on the behaviors, type of behaviors in a great article of Robert Hedgate.

Implementation though attached behavior.

Advantage of attached behavior is that it can be used in a native windows store app. Disadvantage is that it does not show up in Blend under behaviors, and thus cannot be attached if you are using Blend. In my case it’s very important to have possibility of using it on windows store app.

In the implementation of the behavior we have 4 dependency properties:

  • Interval — time interval after which cursor should be hided.
  • IsEnabled — enable or disable behavior functionality.
  • ActiveCursor — cursor type when it’s not active.
  • InactiveCursor — cursor type when it’s active.

Logic isn’t really complicated so you just can download it from my Bitbucket.

Choose control style in XAML through converter.

Frequently we need to change control style (or template) dynamic. Of course it can be done on different ways:

  • Put two controls and “play” with Visibility property and binding – I suppose it’s the less smart case;
  • You can choose DataTemplate/ControlTemplate in code behind file;
  • And use converter.

I’ll show the last one.

The task is: we have a control (in my case it’s just an expand/collapse button) and we need to choose appropriate style for this control which depends on a model.

I created two styles:

  1. True style for expanded state
     <Style TargetType="Button">
                    <Setter Property="Width" Value="13" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Grid Width="13" Height="13" VerticalAlignment="Center" HorizontalAlignment="Center" Background="Transparent" Cursor="Hand">
                                    <Rectangle StrokeThickness="1" Stroke="{Binding ForegroundColor}" RadiusX="2" RadiusY="2" />
                                    <Rectangle Fill="{Binding ForegroundColor}" Height="1" VerticalAlignment="Center" Margin="3" />
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
  1. False style for collapsed state
    <Style TargetType="Button">
                    <Setter Property="Width" Value="13" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Grid Width="13" Height="13" VerticalAlignment="Center" HorizontalAlignment="Center" Background="Transparent" Cursor="Hand">
                                    <Rectangle StrokeThickness="1" Stroke="{Binding ForegroundColor}" RadiusX="2" RadiusY="2" />
                                    <Rectangle Fill="{Binding ForegroundColor}" Height="1" VerticalAlignment="Center" Margin="3" />
                                    <Rectangle Fill="{Binding ForegroundColor}" Width="1" HorizontalAlignment="Center" Margin="3"  />
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>

Next step, I create a BoolToStyleConverter:

    public class BoolToStyleConverter : IValueConverter
    {
        public Style TrueStyle { get; set; }

        public Style FalseStyle { get; set; }

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((bool)value) ? TrueStyle : FalseStyle;
        }

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

        #endregion
    }

Also I create model with following properties:

    public class TaskModel
    {
        public bool IsExpanded { get; set; }

        public Brush ForegroundColor { get; set; }
    }

As a result, I’ve got following behavior only in a XAML. Additionally, I can use a bind (in my case it’s just a foreground color of the model to my style).

Expanded state

Collapsed state

Enjoy!

Disable XAML Designer in Visual Studio 2010

XAML designer is always opened by default in Visual Studio. I don’t like this behavior. Especially because I usually use just Xaml mode without Design mode.

So please don’t waste your time on closing this window each time and switch off it eventually. You can do it by selection following option:

Tools -> Options -> Text Editor -> XAML -> Miscellaneus


And select “Always open documents in full XAML view” under.