GithubHelp home page GithubHelp logo

universalappsample's Introduction

Chat on Gitter

This is walk through of the complexity in using Fody inside a Windows Universal project. The approach used can be applied to any project that uses the new project.json approach.

The Solution

This repository contains a solution with two projects.

AppWithoutFody

A project that does not use Fody and implement sINotifyPropertyChanged manually.

AppWithFody

A "finished" project that does already utilizes Fody, and the weavers PropertyChanged and Fielder, to simplify the PersonViewModel.

Upgrading AppWithoutFody

We will now walk through converting AppWithoutFody to use Fody

1. Install Fody

Run the following on the Package Manager Console

install-package Fody

You want to install the Fody package first since package dependency resolution in nuget defaults to the oldest version. Hence if u install a weaver first you will get a really old version of Fody instead of the newest (desired) version.

Usually when installing Fody the FodyWeavers.xml is deployed via the Content feature of nuget. However in NuGet 3.1 the Content feature was deprecated for projects using the project.json approach.

Hence you will need to manually add this FodyWeavers.xml file to your project.

<?xml version="1.0" encoding="utf-8" ?>
<Weavers>
</Weavers>

2. Install some weaver nugets

Run the following on the Package Manager Console

install-package Fielder.Fody
install-package PropertyChanged.Fody

Usually when you install a Fody addin it will leverage either the install.ps1 or the xdt features of nuget to inject itself into the FodyWeavers.xml. However in NuGet 3.1 support for xdt and install.ps1 was deprecated for projects using the project.json approach.

Hence if you build you will get this error

No configured weavers. 
It is possible you have not installed a weaver or have installed a fody weaver nuget into a project type that does not support install.ps1. 
You may need to add that weaver to FodyWeavers.xml manually. 
eg. <Weavers><WeaverName/></Weavers>. 
see https://github.com/Fody/Fody/wiki/SampleUsage

This is because Fody is trying to tell you something is not configured correctly since it cant find any weavers.

3. Add weavers to xml

So initially add just <Fielder/>

<?xml version="1.0" encoding="utf-8" ?>
<Weavers>
  <Fielder/>
</Weavers>

Now build again

You will get the following error

PropertyChanged expected to be executed. 
You may also need to manually add '<PropertyChanged />' into your FodyWeavers.xml. 
eg <Weavers><PropertyChanged/></Weavers>. See https://github.com/Fody/Fody/wiki/SampleUsage	

This is because while the XDT and install.ps1 was deprecated the import msbuild targets feature still works. So for project.json projects a fody weaver can plug into the msbuild pipeline and ensure that weaving has occurred.

Now add <PropertyChanged/> to fix the error

<?xml version="1.0" encoding="utf-8" ?>
<Weavers>
  <Fielder/>
  <PropertyChanged/>
</Weavers>

4. Simplify the view model

You can now convert the view model over

Before


public class PersonViewModel : INotifyPropertyChanged
{
    string givenNames;
    string familyName;

    public string FullName => GivenNames + " " + FamilyName;

    public string FamilyName
    {
        get { return familyName; }
        set
        {
            familyName = value;
            OnPropertyChanged();
            OnPropertyChanged("FullName");
        }
    }

    public string GivenNames
    {
        get { return givenNames; }
        set
        {
            givenNames = value; 
            OnPropertyChanged();
            OnPropertyChanged("FullName");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

After

[ImplementPropertyChanged]
public class PersonViewModel
{
    public string GivenNames;
    public string FamilyName;
    public string FullName => GivenNames +" " + FamilyName;
}

Summary

The above might seem complex but in reality you just need to remember two things.

  1. Manually add a FodyWeavers.xml when you install Fody
  2. Manually add a weavers name to FodyWeavers.xml when you install it.

But cant you make it simpler

Unfortunately without any extension points exposed by Nuget it is not possible to simplify this process. Feel free to ask the nuget guys to add either xdt or install.ps1 support to project.json projects.

universalappsample's People

Contributors

simoncropp avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.