小编Joh*_*ica的帖子

获取 Json 数组大小

我有两个从 Web API 返回的 Json,其中包含动态大小数组“联系人”和“设施”。

通常我将使用以下代码访问 Json 中的固定大小数组“结果”,因为我正在修复大小。

string json = _obj.GetJsonResult(url);
Hotel hotel = Newtonsoft.Json.JsonConvert.DeserializeObject<Hotel>(json);
hotelName = hotel.results[0].property_name;
Run Code Online (Sandbox Code Playgroud)

我尝试了以下链接和以下代码来获取动态数组大小,但它似乎不起作用。

var jObject = JObject.Parse(json);
JArray jArray = (JArray)jObject["hotel.results[0].contacts"];
int length = jArray.Count;
Run Code Online (Sandbox Code Playgroud)

杰森一号

{
  "results": [
    {
      "property_code": "FGPENOTP",
      "property_name": "Olive Tree Penang",
      "location": {
        "latitude": 5.32708,
        "longitude": 100.27944
      },
      "address": {
        "line1": "No 76 Jalan Mahsuri",
        "city": "Bayan Lepas",
        "postal_code": "11950",
        "country": "MY"
      },
      "total_price": {
        "amount": "265.00",
        "currency": "MYR"
      },
      "min_daily_rate": {
        "amount": "250.00",
        "currency": "MYR" …
Run Code Online (Sandbox Code Playgroud)

c# arrays json amadeus

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

DeflateStream 复制到内存流

Tyring 压缩和解压缩,MemoryStream但似乎CopyTo不像预期的那样工作?为什么?如何解决这个问题?

public static MemoryStream Compress(MemoryStream originalStream)
{
    Console.WriteLine("Original before compressing size: {0}", originalStream.Length.ToString());
    MemoryStream compressedMemoryStream = new MemoryStream();

    using (DeflateStream deflateStream = new DeflateStream(compressedMemoryStream, CompressionMode.Compress, true))
    {
        originalStream.CopyTo(deflateStream);
    }
    Console.WriteLine("Compressed size: {0}", compressedMemoryStream.Length.ToString());
    return compressedMemoryStream;
}

public static void Decompress(MemoryStream compressedStream)
{
    Console.WriteLine("Compressed before decompressing size: {0}", compressedStream.Length.ToString());
    using (MemoryStream decompressedFileStream = new MemoryStream())
    {
         using (DeflateStream decompressionStream = new DeflateStream(compressedStream, CompressionMode.Decompress, true))
         {
              decompressionStream.CopyTo(decompressedFileStream);
         }
         Console.WriteLine("Decompressed size: {0}", decompressedFileStream.Length.ToString());
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

Original before …
Run Code Online (Sandbox Code Playgroud)

c# memorystream deflate

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

如何检测后退按钮点击并使webview返回

我正在尝试使用 Xamarin 为我的一个项目构建一个 webview 应用程序,但我似乎无法弄清楚如何让 webview 元素转到上一页而不是关闭应用程序。

所以我想出了如何检测被按下的后退按钮并防止它关闭应用程序,但我想让网页返回,如果网页无法返回,则关闭应用程序。

这是我目前的代码:

using System;
using Xamarin.Forms; 
using Xamarin.Forms.Xaml; 
using Application = Xamarin.Forms.Application; 

namespace myNewApp
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public class WebPage : ContentPage
    {
        public object _browser { get; private set; }

        protected override bool OnBackButtonPressed()
        {

                base.OnBackButtonPressed();
                return true;

        }

        public WebPage()
        {


            var browser = new Xamarin.Forms.WebView();

            browser.Source = "https://myurl.com";


            Content = browser;

        }



    }
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试了几个答案,我发现了这段代码,但它不起作用,因为覆盖无法访问公共网页浏览器变量:

if (browser.CanGoBack)
            {
                browser.GoBack();
                return true;
            }
            else
            {
                base.OnBackButtonPressed();
                return true;
            }
Run Code Online (Sandbox Code Playgroud)

任何帮助将非常感激。

c# android xamarin

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

无法转换lambda表达式(async/await)

无法将lambda表达式转换为预期的委托类型,因为块中的某些返回类型不能隐式转换为委托返回类型

我想在另一个线程中运行函数,我可以毫无错误地执行此操作,但仅在计算函数不是时void.我想使用void函数.请告诉我该怎么做或它应该返回什么样的结果?

private async void buttonStep3_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = folderBrowserDialog1.ShowDialog();
            if (dialogResult != DialogResult.OK)
                return;

            SetAllButtonsStateEnabled(false);

            progressBar1.Value = 0;
            progressBar1.Visible = true;

            var progressProgressBarValue = new Progress<int>(s => progressBar1.Value = s);

            await Task.Run(() => SizeFilter3(
                                    Convert.ToInt32(textBoxSF1W.Text), Convert.ToInt32(textBoxSF1H.Text),
                                    Convert.ToInt32(textBoxSF2W.Text), Convert.ToInt32(textBoxSF2H.Text),
                                    Convert.ToInt32(textBoxSF3W.Text), Convert.ToInt32(textBoxSF3H.Text),
                                    Convert.ToInt32(textBoxSF4W.Text), Convert.ToInt32(textBoxSF4H.Text),
                                    Convert.ToInt32(textBoxSF5W.Text), Convert.ToInt32(textBoxSF5H.Text),
                                    progressProgressBarValue),
                                TaskCreationOptions.LongRunning);//this line gives an error


            progressBar1.Visible = false;
            progressBar1.Value = 0;

            SetAllButtonsStateEnabled(true);
        }
private void SizeFilter3(int filterW1, int filterH1,
                                   int filterW2, int filterH2,
                                   int filterW3, int filterH3,
                                   int …
Run Code Online (Sandbox Code Playgroud)

c# async-await

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

如何通过脚本切片精灵?(不使用编辑器)

我尝试通过脚本切片 Sprite(类型转换为Texture2D),

当项目在Android或IOS平台上运行时

通过脚本可以吗?

我尝试使用 UnityEditor 类,它可以在计算机上使用

但是当我尝试构建 Android 或 IOS 时却失败了。

    void OnPreprocessTexture()
    {
        TextureImporter textureImporter = (TextureImporter)assetImporter;
        textureImporter.textureType = TextureImporterType.Sprite;
        textureImporter.spriteImportMode = SpriteImportMode.Multiple;
        textureImporter.mipmapEnabled = false;
        textureImporter.filterMode = FilterMode.Point;

    }

    public void OnPostprocessTexture(Texture2D texture)
    {
        Debug.Log("Texture2D: (" + texture.width + "x" + texture.height + ")");



        int spriteSize = 350;
        int colCount = texture.width / spriteSize;
        int rowCount = texture.height / spriteSize;

        List<SpriteMetaData> metas = new List<SpriteMetaData>();

        for (int r = 0; r < rowCount; ++r)
        {
            for (int …
Run Code Online (Sandbox Code Playgroud)

c# unity-game-engine

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

如何使用自定义工厂方法注册开放泛型?

TL;DR:我可以使用 Autofac 创建一个通用工厂,以便我可以注入IProduct<TModel>而不是从IFactory我需要的任何地方解析它吗?有没有办法将解决从工厂任务移动到组合根?

所以我使用第三方库,它公开了一些通过工厂创建的通用接口。出于演示目的,我们假设以下代码是库:

第三方库模型

public interface IFactory
{
    IProduct<TModel> CreateProduct<TModel>(string identifier);
}

internal class Factory : IFactory
{
    private readonly string _privateData = "somevalues";

    public IProduct<TModel> CreateProduct<TModel>(string identifier)
    {
        return new Product<TModel>(_privateData, identifier);
    }
}

public interface IProduct<TModel>
{
    void DoSomething();
}

internal sealed class Product<TModel>: IProduct<TModel>
{
    private readonly string _privateData;
    private readonly string _identifier;

    public Product(string privateData, string identifier)
    {
        _privateData = privateData;
        _identifier = identifier;
    }

    public void DoSomething()
    { …
Run Code Online (Sandbox Code Playgroud)

c# autofac

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

如何获取当前使用的网络适配器

现在我正在尝试在我的机器上的多个网络适配器之间获取正在使用的网络适配器,以便我可以获得网络适配器的ip地址、mac地址和适配器名称。但我没有任何运气。我找到的解决方案是获取当前使用的网络适配器。我不认为它适用于 .Net 框架项目,而是适用于 ASP.NET Core。

我的代码如下。

public void GetIpAndMacAddress()
{
    foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
    {
        // Only consider Ethernet network interfaces
        if (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet &&
            nic.OperationalStatus == OperationalStatus.Up)
        {
            IPInterfaceProperties adapterProperties = nic.GetIPProperties();
            foreach (var ip in adapterProperties.UnicastAddresses) 
            {
                if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    IPAddress = ip.Address.ToString();
            }
            string hexMac = nic.GetPhysicalAddress().ToString(),
                   regex = "(.{2})(.{2})(.{2})(.{2})(.{2})(.{2})",
                   replace = "$1:$2:$3:$4:$5:$6";
            IPv4InterfaceProperties p = adapterProperties.GetIPv4Properties();

            netAdapterName = nic.Name;
            MACAddress = Regex.Replace(hexMac, regex, replace);
            return;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# mac-address ip-address

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

如何在控制台窗口中将字符串设为粗体?

所以我试图找到一个简单的代码,使我的代码以粗体打印。当我在互联网上搜索时,所有这些都非常复杂,甚至不值得。有没有更简单的方法来制作字符串或只是一个 Console.WriteLine(" "); 胆大?

Console.Write("Type in the number of people to create: ");
int time = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\nGenerating " + time + " people please stand by...");
System.Threading.Thread.Sleep(3000);
Console.Clear();
Run Code Online (Sandbox Code Playgroud)

c#

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

如何检查 RGB 颜色以及如何知道此范围是绿色还是红色?

string greenImage = @"C:\Users\keti.p\Desktop\greenImage.jpg";
Bitmap bitMap = new Bitmap(greenImage);
int width = maxWidth / 2;
int height = maxHeight / 2;
Color c = bitMap.GetPixel(width, height);

Color color = Color.FromArgb(c.R, c.G, c.B);
Run Code Online (Sandbox Code Playgroud)

我有这个“颜色”对象,

例如,这个颜色是绿色的吗?或者它是红色的?或者?绿色的护林员真好。。

c# rgb colors

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

为什么返回 null 在 web api 项目中没有任何作用?

我是 asp.net core 3 的新手,很抱歉,如果我的问题听起来太基本了,下面是我的控制器类:

[ApiController]
[Route("[controller]")]
public class MyController : ControllerBase {
   List<string> _fruit = new List<string> { "Pear", "Lemon", "Peach" };
  
   [HttpGet("fruit")]
   public IEnumerable<string> MethodXXX() {
      return _fruit;   
   }
   ...
}
Run Code Online (Sandbox Code Playgroud)

所以当我路由到 时my/fruit,我得到一个字符串列表。

但如果我将 MethodXXX 更改为:

[HttpGet("fruit")]
public IEnumerable<string> MethodXXX() {
   return null;
}
Run Code Online (Sandbox Code Playgroud)

然后浏览器总是回退到以前的网址,例如我一my/other开始就在,然后我将网址更改为my/fruit,我可以看到浏览器发送了一个新请求,并且网址my/fruit在短时间内更改为 ,然后它再次落回my/other

这种行为是为了什么?我很确定我读过一本书,上面说你可以从操作方法返回 null?

c# .net-core asp.net-core

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