小编M35*_*579的帖子

在C#UWP中创建Web服务器

我正在用C#编写一个Web服务器作为通用Windows平台应用程序.到目前为止,这是我的代码:

sealed partial class App : Application
    {
        int port = 8000;

        /// <summary>
        /// Initializes the singleton application object.  This is the first line of authored code
        /// executed, and as such is the logical equivalent of main() or WinMain().
        /// </summary>
        public App()
        {
            StartServer();
        }

        private void StartServer()
        {
            StreamSocketListener listener = new StreamSocketListener();
            listener.BindServiceNameAsync(port.ToString());
            Debug.WriteLine("Bound to port: " + port.ToString());
            listener.ConnectionReceived += async (s, e) =>
                {
                    Debug.WriteLine("Got connection");
                    using (IInputStream input = e.Socket.InputStream)
                    {
                        var …
Run Code Online (Sandbox Code Playgroud)

c# webserver uwp

6
推荐指数
2
解决办法
8715
查看次数

应用关闭时运行的代码

我发现了很多编写代码的例子,这些代码在WPF或Windows Forms应用程序终止时执行,但不适用于UWP应用程序.是否有可以覆盖的特殊C#方法,或者可以用来包含清理代码的事件处理程序?

这是我尝试的WPF代码,但在我的UWP应用程序中不起作用:

App.xaml.cs (没有样板用法和名称空间声明)

public partial class App : Application
{
        void App_SessionEnding(object sender, SessionEndingCancelEventArgs e)
        {
            MessageBox.Show("Sorry, you cannot log off while this app is running");
            e.Cancel = true;
        }
}
Run Code Online (Sandbox Code Playgroud)

App.xaml中

<Application x:Class="SafeShutdownWPF.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:SafeShutdownWPF"
             StartupUri="MainWindow.xaml"
             SessionEnding="App_SessionEnding">
    <Application.Resources>

    </Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)

我尝试使用Process.Exited,但VS2015无法识别内部的进程System.Diagnostics.

c# win-universal-app

5
推荐指数
1
解决办法
903
查看次数

在Windows UWP应用程序中使用Python和C#

我开始用Python编写应用程序,但我现在想切换到C#和UWP.我知道你不能用Python编写UWP应用程序,但我试图看看我是否可以用Python编写一些代码并从C#访问该代码.

例如,在Python中编写一个C#代码也可以访问的类.那可能吗?如果是这样,Python可以访问微软的UWP API吗?

主代码不会用Python编写; 那是不可能的.但是,C#和Python之间是否存在互操作性,可能还有IronPython(.NET的Python)?

我将如何设置这样的Visual Studio项目?我安装了Visual Studio for Python Studio,但没有内置选项将Python文件添加到我的UWP应用程序中.

c# python ironpython language-interoperability uwp

5
推荐指数
1
解决办法
3209
查看次数

你能在UWP的C#代码中使用C++ DLL吗?

我在Visual Studio中编写了一个C++类库,它只定义了一个调用Python的函数:

#pragma once

#include <Python.h>

extern "C"
__declspec(dllexport)
void python()
{
    Py_Initialize();
    PyRun_SimpleString("2 + 2");
}
Run Code Online (Sandbox Code Playgroud)

我在同一个C#Blank Universal应用程序的解决方案中创建了另一个项目.我试图引用我之前提到的项目生成的DLL:

using System;
...

namespace StartupApp
{
    ...
    sealed partial class App : Application
    {
        private const string CPPPythonInterfaceDLL = @"pathtodll";

        [DllImport(CPPPythonInterfaceDLL, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
        private static extern void python();

        public static void Python()
        {
            python();
        }
        ...
        public App()
        {
            ...

            Python();
        }

        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

该应用程序处于发布配置中.

每当我尝试在本地计算机上运行应用程序时,它总是会出错:

The program '[2272] StartupApp.exe' has exited with code -1073741790 (0xc0000022). …
Run Code Online (Sandbox Code Playgroud)

c# c++ python dll uwp

4
推荐指数
1
解决办法
8469
查看次数

C#通用Windows StreamSocket客户端未连接到StreamSocketListener服务器

我正在尝试使用C#和UWP编写一个简单的套接字服务器.到目前为止,我已经在C#中编写了一个可以子类化的SocketServer类; 我希望它像Node.js中的套接字服务器.

但是,我尝试从Python REPL和C#客户端连接到此服务器,但两次连接都超时.我将我的代码与Github上的Microsoft的StreamSocket示例进行了比较,我无法弄清楚我做错了什么.我不知道我的代码或我的计算机上的某些网络配置是否有问题.

这是我的服务器超类:

...    
    abstract class SocketServer
    {
        public SocketServer()
        {

        }

        public async void Listen(String port)
        {
            StreamSocketListener serverSocket = new StreamSocketListener();
            await serverSocket.BindServiceNameAsync("8003");

            serverSocket.ConnectionReceived += ClientConnectionReceived;

            Debug.WriteLine("Server started");
        }

        private void ClientConnectionReceived(
            StreamSocketListener sender,
            StreamSocketListenerConnectionReceivedEventArgs args)
        {
            Debug.WriteLine("Client connection received");

            OnConnection(args.Socket);

            DataReader reader = new DataReader(args.Socket.InputStream);

            try
            {
                // fill in later
            }
            catch
            {
                // fill in later
            }
        }

        abstract protected void OnConnection(StreamSocket socket);

        abstract protected void OnData(String data);

        abstract protected void OnEnd(); 
    } …
Run Code Online (Sandbox Code Playgroud)

c# sockets server uwp

2
推荐指数
1
解决办法
3256
查看次数