How to stop Facebook from stealing your attention

There are times when you check Facebook to just fill a free minute or post something and then you find you’ve been pulled into the vortex of links worth clicking. You can unfollow everyone and everything until your timeline slows down, but then they give you trending topics, suggested pages, ads etc. Well, today you can switch your Facebook from looking like this:
Continue reading

Tagged , , , ,

High performance XAML charts… using Win2D

ScreenCap

Hi! 

Over the last couple of years I got quite a few questions about the Chart control that I ported from Silverlight Toolkit and shared in WinRT XAML Toolkit. Mostly about performance and customizing the design. Well, the update performance is something I recently tweaked a little bit by removing some of the animations, but really – the control is fairly complicated and uses lots of XAML to enable customizing all the different parts and so it works best for static charts with just a few data points. I haven’t actually used the control much, so every time I get asked how to change anything in it – I have to investigate it myself and often get lost in all that code.

Continue reading

Tagged , , ,

Setting and checking flags in MSBuild projects

I’ve been looking into ways to temporarily stop trying to access a network share for a while if I can’t access it in an MSBuild project. The thing is – sometimes trying to access a network share takes a really long time and if your target is executed many times by various projects – it could really slow things down and if accessing that share is only desired, but not required – it would be bad to slow things down so badly. One way I thought was interesting was to set a file or registry value as a flag based on the date, so that I will stop trying for the day, but retry the next day. Here’s how I’ve done it:

File flag

<PropertyGroup>
    <TodaysDate>$([System.DateTime]::Now.ToString("yyyyMMdd"))</TodaysDate>
    <ShareErrorFlagFile>ShareAccessError_$(TodaysDate).flag</ShareErrorFlagFile>
    <SharePath
        Condition="
            '$(SharePath)' == '' And
            !Exists($(ShareErrorFlagFile)) And
            Exists('\\machine\folder')">\\machine\folder</SharePath>
    <SharePath
        Condition="
            '$(SharePath)' == '' And
            !Exists($(ShareErrorFlagFile)) And
            Exists('\\machine2\folder')">\\machine\folder</SharePath>
</PropertyGroup>

<Target
    Name="test">
    <CallTarget
        Condition="'$(SharePath)' == ''"
        Targets="HandleShareAccessError" />
    <CallTarget
        Condition="'$(SharePath)' != ''"
        Targets="DoSomething" />
</Target>

<Target
    Name="HandleShareAccessError">
    <Message
        Text="Could not reach SharePath"
        Importance="high" />
    <Exec
        Command="echo Failed to access build tracker install share today. Delete this file to try again. $(ShareErrorFlagFile)" />
</Target>

Registry value

<PropertyGroup>
    <TodaysDate>$([System.DateTime]::Now.ToString("yyyyMMdd"))</TodaysDate>
    <SharePath
        Condition="
            '$(SharePath)' == '' And
            '$(registry:HKEY_CURRENT_USER\SOFTWARE\Contoso\MyApp@DisabledForDay)' != '$(TodaysDate)' And
            Exists('\\machine\folder')">\\machine\folder</SharePath>
    <SharePath
        Condition="
            '$(SharePath)' == '' And
            '$(registry:HKEY_CURRENT_USER\SOFTWARE\Contoso\MyApp@DisabledForDay)' != '$(TodaysDate)' And
            Exists('\\machine2\folder')">\\machine\folder</SharePath>
</PropertyGroup>

<Target
    Name="test">
    <CallTarget
        Condition="'$(SharePath)' == ''"
        Targets="HandleShareAccessError" />
    <CallTarget
        Condition="'$(SharePath)' != ''"
        Targets="DoSomething" />
</Target>

<Target
    Name="HandleShareAccessError">
    <Message
        Text="Could not reach SharePath"
        Importance="high" />
    <Exec
        Command="reg add HKCU\Software\Contoso\MyApp /v DisabledForDay /t REG_SZ /d $(TodaysDate) /f" />
</Target>

The registry key solution is slightly cleaner – it doesn’t generate date-stamped files in every place you run it, but the file-based one is more discoverable and so when you clean the directory where you run it – it will try to access the share again.

Tagged

Exiting a WPF app

So this is a developing story. I’m working on a WPF app again these days and how do you exit a WPF when you’re done running? Normally a user or your code might close the main/last window and the app would exit, but what else can you do?

  • Application.Current.Shutdown() is what I’ve been trying and it wouldn’t work. Why? It turns out – it needs to be invoked on the dispatcher thread… ugh. Anyways – it seems to be THE WPF way, so I’m sticking with it:
    Application.Current.Dispatcher.Invoke(Application.Current.Shutdown);
  • Environment.Exit(0) should work WPF or not.
  • Process.GetCurrentProcess().Kill() is a rather sudden, but sure way to kill your own process.
Tagged ,

Cannot deserialize XBF metadata type list as NullableBoolToBoolConverter was not found in namespace

I was trying to get something working last night and kept hitting this exception when my app was trying to InitializeComponent() at one point.

CannotDeserializeXbf

First-chance exception at 0x750D1D4D in App1.exe: Microsoft C++ exception: Platform::COMException ^ at memory location 0x037CDDC0. HRESULT:0x802B000A The text associated with this error code could not be found.

WinRT information: Windows.UI.Xaml.Markup.XamlParseException: The text associated with this error code could not be found.

Cannot deserialize XBF metadata type list as ‘NullableBoolToBoolConverter’ was not found in namespace ‘WinRTXamlToolkit.Converters’. [Line: 0 Position: 0]

at Windows.UI.Xaml.Application.LoadComponent(Object component, Uri resourceLocator, ComponentResourceLocation componentResourceLocation)

All this seemed complete and utter bogus, since NullableBoolToBoolConverter was obviously there and it would work perfectly well in other projects or solution configurations, so let’s look what was happening.

  • NullableBoolToBoolConverter is defined in WinRTXamlToolkit – a .NET Class Library that I’d typically build as Any CPU. It has a ton of controls, converters and other such goodies and of course it typically works just fine.
  • NullableBoolToBoolConverter is used in WinRTXamlToolkit.Debugging – another .NET Class Library that has an in-app XAML visual tree debugger tool – essentially a TreeView control that displays information about the visual tree of your application – hierarchy of UI elements and their properties. The main control in the library uses NullableBoolToBoolConverter and it normally works when used from a .NET Windows Store app.
  • The problem is that WinRTXamlToolkit.Debugging is a .NET Class Library and there is still a small, but important range of WinRT XAML apps written in C++ that can’t use it, so I created a managed WinRT component library – WinRTXamlToolkit.Debugging.WinRTProxy that C++/CX projects can reference. It’s fairly straightforward to do it seems – you just create a proxy class in the WinRT Component library that has methods that invoke methods in the referenced class library (WinRTXamlToolkit.Debugging) and now you can use it from C++/CX. I prefer to keep WinRTXamlToolkit.Debugging a regular .NET class library because WinRT Components have limitations that would limit the APIs I currently have in WinRTXamlToolkit.Debugging. The proxy methods in the WinRTProxy library still have these limitations, but at least I can use it from a native app. The problem is that it wouldn’t work and keep giving me that exception, so what’s up?

It turns out WinRT XAML generates these files – XamlTypeInfo.g.cs that allow the XAML parser to see the types it can use and I noticed that file was missing in the obj folder of WinRTXamlToolkit.Debugging.WinRTProxy. To get it to generate I simply added one empty UserControl to WinRTXamlToolkit.Debugging.WinRTProxy and everything started working fine! Weird but it works. So now I’m left with figuring out how to wrap this all as a NuGet component with native versions of the .NET libraries, since I’ve previously only packaged Any CPU binaries…

Tagged , , , , , ,

The Elusive ArgumentException in a List Control…

The last week or so I have been struggling with a bug that was keeping me away from updating WinRT XAML Toolkit. After adding an ItemTemplateSelector for my custom ListBox – I started getting ArgumentExceptions with a vague “Value does not fall within the expected range.” message and of course none of my code on the stack. At first I thought my custom ListBox subclass was causing some problems that the platform couldn’t handle, but then after replacing it with a regular ListBox it kept happening. Finally I noticed that replacing the DataTemplate I was using with another one that was simpler stopped the exception. Then I modified that template to look the same as the one I wanted to use – and it still worked fine! It took me probably 2 minutes of playing spot the difference to notice that the bad template had a DataTemplate inside of the DataTemplate! That obviously doesn’t generate a good list item! Hopefully this post helps someone working with a ListBox, ListView or GridView and getting these ArgumentExceptions that crash the app with no details of where they come from…

Tagged ,

Switching Project Type Between Windows Runtime Component and (.NET) Class Library

There are times when you might want to switch the type of your library between a Windows Runtime Component and (.NET) Class Library. Why would you do that? There are slight differences between these – a WinRT Component can be used by other assemblies written in any language supported by WinRT – your APIs will be projected to these other languages. A Class Library can only (easily) be used by .NET assemblies, but allows you to have class inheritance. Depending on the realization or change of your requirements then you might decide that you need to change the type of the library you have already created, but how do you do that? Easily. Assuming your code is compliant with the above mentioned limitations you just need to change the value of one XML element in your csproj file between <OutputType>winmdobj</OutputType> and <OutputType>Library</OutputType>.

*EDIT

Silly me, how could I have overlooked it? The project properties panel has an option for switching between Class Library, WinRT Component and Windows Store App!

Switching project types between Class Library, Windows Runtime Component and Windows Store App in Visual Studio

Switching project types between Class Library, Windows Runtime Component and Windows Store App in Visual Studio

How to safely use your time while you build a project?

So your build takes a while… How do you make use of that time (e.g. check Stack Overflow) and not forget to get back to work when the build is done? Well, you can make a sound. I am not sure if there is a simple built-in option to Visual Studio allows to play sounds on events. It is quite possible there is, but I will use what I know. I found you can play a sounds in command line by typing echo ^G, but it didn’t seem to work for me in a VS project post-build step. I blogged about using batch scripts and C# code together in a single file and here’s a way to beep from a batch script:

Continue reading

Tagged , ,

The five minute gauge control for WinRT/XAML

Wow, gauge controls are pretty easy to implement. Why would anyone pay for one?🙂

This one took me literally 5 minutes. True, it is just a UserControl and it only shows 0-360 values I needed to debug some compass code, but it’s easy to build on it so I thought I’d share it since all my other blog post ideas need quite a bit more time than 5 minutes…🙂

gauges

Continue reading

Tagged , , , ,

Code Snippets – Disable Warning 4014

I use code snippets extensively. I don’t think it makes sense to do without them if you do any XAML work, though what you use them for is actually the code behind where lots of boilerplate code is necessary. My snippets collection consists mostly of modified Dr WPF’s dependency property snippets and various types of observable property snippets. Today I am working on cleaning up some code in WinRT XAML Toolkit and decided a snippet to disable warnings could be helpful, so here it is (also added to my collection on dropbox). Just use the sw shortcut for this Suppress Warning snippet.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <SnippetTypes>
        <SnippetType>SurroundsWith</SnippetType>
      </SnippetTypes>
      <Shortcut>sw</Shortcut>
      <Title>Suppress Warning</Title>
      <Description>Surrounds with a #pragma warning disable/restore block.</Description>
      <Author>Filip Skakun</Author>
      <HelpUrl>https://xyzzer.wordpress.com</HelpUrl>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>warninglist</ID>
          <ToolTip>Comma separated warning list.</ToolTip>
          <Default>4014</Default>
        </Literal>
      </Declarations>
      <Code Language="csharp">
        <![CDATA[#pragma warning disable $warninglist$
$selected$$end$
#pragma warning restore $warninglist$]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>