c# - Is there any way to share code between UWP apps and WPF apps? -
to clear, follow mvvm pattern, , want structure project such can share model code between uwp app , standard wpf app. code want share has no ui. don't relish thought of finding new tools replace ones i've been using years take care of tasks logging, connecting document oriented database, etc.
i attempted start writing uwp wrapper around code have , reference model project directly. visual studio refused let happen, showing me error message says "unable add reference project 'ack.model'". same thing happened when attempted put model in universal library , reference wpf app. i'm not trying share wpf code. model layer has no reference ui libraries.
this scary proposition, because means if want substantial have choose either jump 100% uwp or stay 100% wpf. newtonsoft.json might have universal distribution (asp.net mvc), elasticsearch.net, , other tools needed make important apps?
i found "portable class library" project type hiding. pcls allow me share my code across wpf , universal apps 1 of options. solves simple case of model part of code, (still) can't use of libraries want. there still large number of libraries need not have pcl available.
about year later, advent of visual studio 2017 there more complete solution. if target libraries .net standard library compatible both .net core apps , monolithic .net targeted app. support standard .net libraries , apis complete, support modern c# language features.
the general advice this:
- target .net standard libraries
- target appropriate platform actual application. (uwp or wpf).
note: if library has interact c libraries or applications, have take care make sure load correct version.
it appears there solution, has adopted whole tool chain want use. when microsoft introduced windows store apps in windows 8, introduced portable class library (pcl). purpose of pcl share code between different parts of application.
when create pcl in visual studio 2015, can specify types of apis want accessible from:
- universal apps
- mono
- .net core 5
- .net 4.6
this of course, limits apis available of ones want use ok long it's not ui related. there other limitations well:
- your project can edited in visual studio 2015 or greater
- you don't have access special directories environment variable (i.e. user documents directory, etc.)
- you can't link library designed 1 of target platforms (i.e. libgit2sharp, etc.)
there's no way browse api subset--msdn needs on stick.msdn has updated of api documentation, it's still difficult figure out applies pcl
however, can link library designed single target platform pcl. it's not ideal, it's better nothing.
the asp.net mvc stack has been ported using pcls, can use newtonsoft.json directly other of libraries used application. however, there several libraries have not been ported.
this arrangement forces think how want integrate better. .net core 5 seems stable, support in it's infancy. current generation of universal apps of vs 2015 update 1 uses .net core 5 directly.
there several features nuget not supported though work under way:
- ms build extensions (major changes msbuild , project.json structure)
- install/uninstall scripts (related removal of concept of install)
- content (related install/uninstall, work in progress on this)
- content transforms (related lack of install/uninstall)
i wish had more complete answer. far got once discovered pcl , how evolved current infrastructure.
i'm in process of creating game creation toolkit incorporates version control right off bat. want able deploy game windows 10 app, or standard wpf app, due libraries i'm using integrate version control need create editor standard wpf app. had bit creative in building shared code , importing correct libraries.
first, project hierarchy:
- project.model (portable class library)
- project.model.versioning (standard c# library)
- mvvm.toolkit (portable class library)
- editor (standard wpf application)
i want core pcl able load project , deserialize json encoded objects. pcl did have access system.io
, surprisingly not same 1 defined in standard c# library. here's how had fix things:
after adding package reference newtonsoft.json, had change target framework in
packages.config
file:<package id="newtonsoft.json" version="8.0.2" targetframework="portable-net452+win81" />
all projects dependent on project.model class had install `system.io.filesystem' package nuget system.io.fileinfo etc. objects same.
while not panacea, it's not dead end. i'm sure there more gotchas, @ least of problems.
Comments
Post a Comment