简体   繁体   中英

Create a dll from a Windows 8.1 / Windows Phone 8.1 application with views and database

I'm creating a Windows Phone 8.1 app that's supposed to act as a Library for other Windows Phone 8.1 applications. In my new apps I can add the Library project to the solution and thus it functions as a kind of external Library and this does what I want it to do.
However, I would like to know if there is some way of exporting my WP Library app to, for example, a .dll that I could add as reference to my new applications. Is there any other way I could accomplish this?

It is worth mentioning that my Library App will have views and even a database, not just methods to deal with the data from the main application.

Since it is a Windows 8.1 / Windows Phone 8.1. You can take a look at the Store Apps -> Universal Apps -> Class Library (Portable for Universal Apps).


在此输入图像描述

After compiling it to a dll, just add a reference to said dll to the reference folder.

To use the your library in C# , just type in your namespace that you use to create the library.

using your_namespace_from_library;

To use your Views and what not from XAML, just type in your namespace in the XAML file as well

<Page xmlns:YOUR_AWESOME_TAG="using:your_namespace_from_library">

Then you can use your tags from the library by doing this:

<YOUR_AWESOME_TAG:Your_View>

:)


To solve the Payload problem check out the error message, it should something similar to this:

在此输入图像描述

Look at it very carefully. It is trying to reference a directory that doesn't exist to get the right files. The solution (the only one I know of) is to go to that directory and make it. In my case, it is "C:\\Users\\Duan\\Documents\\Visual Studio 2013\\Projects\\Chubosaurus.Charts_Old2\\Chubosaurus.Charts\\bin\\Debug\\Chubosaurus.Charts"

For some reason it decided to create an extra directory in the Debug folder where the dll is compile to. So create that Folder which is the name of the library. And this is the part you're not going to like. Copy the entire content of the Debug folder into that directory you just created (mostly importantly, the Theme folder).

Run your program again, your issues will be resolve. But unfortunately, you have to do this every time you compile that dll project . This is why I said just add the library project as part of the solution until you're ready to package it up into NuGet. Hope that helps.


A Simple Library (Tutorial)

Say I want to make a Portable Library for charting call ChubCharts , I create the Project and it loads me up with a default Portable library with a file call Class1.cs

First thing I do is delete that file :)

Then I add in a Template Control from Add -> Add New Item -> Templated Control

I name that ChubosaurusCharts it should look like this once it's generated.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;

public sealed class ChubosaurusCharts : Control
{
    public ChubosaurusCharts()
    {
        this.DefaultStyleKey = typeof(ChubosaurusCharts);
    }
}

Now I have the most simple control ever. Lets apply a ContentTemplate to it so I can make into a Composite Control so it will actually have something to do :)

Visual Studios makes a folder call Themes inside the solution. Inside that folder is a file named "Generic.xaml" which contains our template.

在此输入图像描述

Now I want to edit the content I will be putting in my Custom Control

Generic.xaml

<Style TargetType="local:ChubosaurusCharts">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:ChubosaurusCharts">
                <Border
                    Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}">
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

By default it just a plain old <Border> :(, I don't want that. Since this is a Charting Library. I would like a few things added to the Visual Tree, mainly a <Canvas> , so lets change that into:

<Style TargetType="local:ChubosaurusCharts">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:ChubosaurusCharts">
                <Canvas Background="{TemplateBinding Background}" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" DataContext="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}}">
                    <ContentPresenter Content="{Binding Surface}"></ContentPresenter>
                </Canvas>                    
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

By now you should of guess you can add as many XAML tags as you want that you need to display your Control correctly. I just want a <Canvas> .


Now with a little DataBinding Magic, I bind the Canvas to the canvas of my control for easy access :)

public sealed class ChubosaurusCharts : Control
{
    public ChubosaurusCharts()
    {
        this.DefaultStyleKey = typeof(ChubosaurusCharts);
        this.Surface = new Canvas();
    }

    private Canvas surface;

    public Canvas Surface
    {
        get
        {
            return surface;
        }
        set
        {
            surface = value;
        }
    }
}

Now it's ready to use :D

Add the Portable Library to your solution added in the namespace. Compile.

Omitting crazy grade-school algebra and my RenderFunction (which just adds lines to the canvas)

Add our custom control to MainPage.xaml, you can also drag the control from the Toolbox now :D

<chubo:ChubosaurusCharts x:Name="my_chart">

And the result of your hard work is :

在此输入图像描述

Cheers :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM