Missing Packages After Cloning

Just a quick post!

Ever clone your .NET solution to a new computer and find that all of your nuget packages are missing?

Right-clicking on your solution and selecting "Restore Nuget Packages" doesn’t seem to do anything? This happens to me and then I never remember the command and have to google it. This time, I am going to write it down!

How To Do It

Open up your Package Manager Console window. It should be on the bottom, but if it isn’t, in your pull down menus, goto Tools | Nuget Package Manager | Package Manager Console. This was for Visual Studio 2019, but probably the same for 2017.

At the command prompt type:

update-package -reinstall

And that’s it! You should find all your annoying yellow warning triangles gone.

Nuget Versioning In Your Library

I was building up a library this morning, and I wanted to use Json.NET with it for deserializing. I was planning on using this library for a number of different apps, and it made me wonder, if I add a dependency such as Json.NET, will it cause problems if I am already consuming it in my app?

Nuget Versioning

For my library, version 10 or higher should be sufficient. I dug into the nuget docs and found a bunch of handy examples.

I installed 10.0.3 into my library project. In the project file itself, I changed the reference version from 10.0.3 to be just 10.0. According to the nuget docs, this will enable 10.0 to be the minimum version, and if there are higher versions, they will be accepted.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="a_class_file.cs" Link="a_class_file.cs" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="10.0" />
  </ItemGroup>
</Project>

Other Versioning

Some other helpful items:

Notation Description
Version="[10.0,11.0]" includes anything from 10.0 to 11.0 including 10.0 and 11.0
Version="(,11.0]" Includes anything up to and including 11.0
Version="[11.0]" Only accept version 11.0

There are lots of other helpful examples in the documentation.

Xamarin Forms: iOS and SIGABRT

Introduction

I have been working on a new project, one that involves Xamarin Forms and communicating with IOT devices over BluetoothLE. BluetoothLE is a very new thing for me, and I have been struggling a bit with getting it setup.

Plugin

Before I go into the SIGABRT issue, I want to give a shoutout to Allan Ritchie @aritchie for his bluetoothle plugin. I have definitely learned a lot from it.

SIGABRT

Did I mention that I am not that familiar with iOS dev work? Or Apple for that matter? When starting up my application, it crashed the moment I tried to access the BLE adapter with this message:

Got a SIGABRT while executing native code. 
This usually indicates a fatal error in the mono runtime or one of the native libraries.

I had no idea what this actually meant. I had the above library sample app and it worked fine. I did some digging and found a post on the Xamarin Forms forums which said this usually corresponds to a privacy issue. And you can use the device logs to figure out which one. I wish I had kept that post so I could attribute it better.

I am using Visual Studio, so in the View | Other Windows there is a Device Log that will give you a view into your device. Be aware, it is literally a fire hose: stop and clear the log before you start your app and it will hopefully reduce some of the clutter.

I did find out that it was indeed a permission issue! I needed to add NSBluetoothPeripheralUsageDescription key and text prompt to the info.plist file. Once added, I was back in business.

Forge DevCon 2018

Well, it has been a while since I have written anything here. It is time to get back at it a bit. One of the highlights of my year last year was being able to present at the Autodesk Forge DevCon in Las Vegas in November of 2018. That was definitely stepping outside of my comfort zone to speak in front of so many people. I was fortunate to be able to co-present with Michael Beale of Autodesk and he lead me through the whole process.

My idea was using progressive web app technology to use some of the Autodesk Forge Cloud platforms while temporarily offline. Specifically how you could use the fetch and cache api’s to pull down the files required for the viewer to function and then view the data while offline.

If this sounds interesting, feel free to check out the video below.

Creating Flexible Offline Workflows Using Autodesk Forge

WPF – Bring the Loaded Event to MVVM

In the spirit of bringing the “Closing” event to your view model, I also decided that I wanted an easy way to bring the “Loaded” event to my view model. This one I don’t like as much as many frameworks have some kind of navigation that can take care of this for you. But if you have a simple app and want to initialize your view model when your window is loaded, you might find this helpful.

Just like in the previous article, you could wire into the “Loaded” event on each window and then delegate to the view model. This does work, but does tend to repeat the same code over and over again. Instead of this I went in and did a solution that is similar to the “Closing” event.

First up is create an interface that defines an Action that will be called when the window is loaded.

public interface ILoadedAction
{
    void WindowLoaded();
}

And a delegate based implementation of the ILoadedAction interface. I like this because it probably takes care of 99% of my cases.

public class DelegateLoadedAction : ILoadedAction
{
    public Action LoadedActionDelegate { get; set; }

    public DelegateLoadedAction()
    {
    }

    public DelegateLoadedAction(Action action)
    {
        LoadedActionDelegate = action;
    }

    public void WindowLoaded()
    {
        LoadedActionDelegate?.Invoke();
    }
}

And finally, some attached properties for our Window.

public class LoadedBindings
{
    public static readonly DependencyProperty LoadedEnabledProperty =
        DependencyProperty.RegisterAttached(
            "LoadedEnabled",
            typeof(bool),
            typeof(LoadedBindings),
            new PropertyMetadata(false, new PropertyChangedCallback(OnLoadedEnabledPropertyChanged)));

    public static bool GetLoadedEnabled(DependencyObject sender) => (bool) sender.GetValue(LoadedEnabledProperty);
    public static void SetLoadedEnabled(DependencyObject sender, bool value) => sender.SetValue(LoadedEnabledProperty, value);

    private static void OnLoadedEnabledPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        if (sender is Window w)
        {
            bool newEnabled = (bool) e.NewValue;
            bool oldEnabled = (bool) e.OldValue;

            if (oldEnabled && !newEnabled)
                w.Loaded -= MyWindowLoaded;
            else if (!oldEnabled && newEnabled)
                w.Loaded += MyWindowLoaded;
        }
    }

    private static void MyWindowLoaded(object sender, RoutedEventArgs e)
    {
        ILoadedAction loadedAction = GetLoadedAction((Window) sender);
        loadedAction?.WindowLoaded();
    }


    public static readonly DependencyProperty LoadedActionProperty =
        DependencyProperty.RegisterAttached(
            "LoadedAction",
            typeof(ILoadedAction),
            typeof(LoadedBindings),
            new PropertyMetadata(null));

    public static ILoadedAction GetLoadedAction(DependencyObject sender) => (ILoadedAction) sender.GetValue(LoadedActionProperty);
    public static void SetLoadedAction(DependencyObject sender, ILoadedAction value) => sender.SetValue(LoadedActionProperty, value);
}

I created two attached properties, one to enable or disable this feature and one to bind to the interface that contains the action to execute when the window is loaded. You could make this even simpler by removing the enabled/disabled property, but I like the flexibility of being able to turn it on and off. If you did want to do that, you would have to define a DependencyPropertyChangedEvent for the LoadedActionProperty and subscribe and unsubscribe to the Loaded event there.

To use these properties, open up your window XAML file, add in the namespace declaration and define your properties.

<Window x:Class="SampleApp.MainWindow"
    xmlns:local="clr-namespace:SampleApp"

    local:LoadedBindings.LoadedEnabled="True"
    local:LoadedBindings.LoadedAction="{Binding LoadAction}">
</Window>

In the sample above I removed some of the other declarations and assumed that our attached properties were in the same namespace as our MainWindow class.

Next, in the view model class, we simply do this:

public class MainWindowViewModel : BaseViewModel
{
    /// constructor, properties etc ...
    /// ...

    private DelegateLoadedAction _loadAction = null;
    public DelegateLoadedAction LoadAction
    {
        get
        {
            return _loadAction ??
                (_loadAction = new DelegateLoadedAction(
                    () =>
                    {
                        /// do your window initialization here
                    }));
        }
    }
}

And that is about it. Now you don’t have to subscribe the Loaded event explicitly any longer and you can consolidate your code into the view model. You can also move any of those longer running operations that were in the constructor of the view model out.

Perhaps the next step will be to turn this and the Closing event code from the previous post into a nuget package that can be just installed into a project.