小编Jim*_*Jim的帖子

一种计算列表中出现次数的方法

有没有一种简单的方法可以将列表中所有元素的出现次数计入C#中的相同列表?

像这样的东西:

using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;

string Occur;
List<string> Words = new List<string>();
List<string> Occurrences = new List<string>();

// ~170 elements added. . . 

for (int i = 0;i<Words.Count;i++){
    Words = Words.Distinct().ToList();
    for (int ii = 0;ii<Words.Count;ii++){Occur = new Regex(Words[ii]).Matches(Words[]).Count;}
         Occurrences.Add (Occur);
         Console.Write("{0} ({1}), ", Words[i], Occurrences[i]);
    }
}
Run Code Online (Sandbox Code Playgroud)

c# list count match .net-3.5

45
推荐指数
3
解决办法
9万
查看次数

Visual Studio代码编译错误 - 必须配置launch.json

我正在尝试使用visual studio代码在Windows 7上编译c#代码.我已下载所有扩展程序但收到此错误:

启动:程序'启动:必须配​​置launch.json.将"program"更改为您要调试的可执行文件的路径.

我无法弄清楚如何解决它.这是我认为需要在launch.json文件中更改的行,这是目前的情况:

"program":"$ {workspaceRoot} /bin/Debug/netcoreapp1.0/exam 1.dll"

(考试1,因为这是我的.cs文件的名称,包含我的C#代码)

当我进入我的.cs文件所在的文件夹时,这是整个路径:

  • "C:\ Users\Kdrumz\Desktop\ObjectOriented\exam 1.cs".

我很迷茫.另外,在使用visual studio代码时,我是否总是必须这样做?任何帮助是极大的赞赏!

使用1.7版的visual studio代码

c# visual-studio-code

10
推荐指数
1
解决办法
4026
查看次数

UserControl InputBindings仅在首先按下按钮后才能工作

通过单击按钮,按钮工作正常.

问题:第一次加载UserControl并且我没有按任何按钮时,Keybinds无法正常工作.手动单击按钮后,键绑定按预期工作.显然我想让用户在任何按钮按下之前使用keybind :)

(我已经尝试将注意力集中在不同的元素上,例如按钮本身)

示例代码,我如何设置我的命令:(使用MVVM-light工具包)

ContextBinding

DataContext="{Binding GameInfoViewModel, Source={StaticResource Locator}}"
Run Code Online (Sandbox Code Playgroud)

视图

<UserControl.InputBindings>
    <KeyBinding Key="Right" Command="{Binding NextCommand}"/>
</UserControl.InputBindings>
//...
<mui:ModernButton Name="ModernButtonNext" IconData="{StaticResource NextIcon}" Command="{Binding NextCommand}" Margin="16 0 0 0" EllipseDiameter="24" IconWidth="14" IconHeight="14" ToolTip="Next image"/>
Run Code Online (Sandbox Code Playgroud)

视图模型

private RelayCommand _nextCommand;

/// <summary>
/// Gets the NextCommand.
/// </summary>
public RelayCommand NextCommand
{
    get
    {
        return _nextCommand ?? (_nextCommand = new RelayCommand(
            ExecuteNextCommand,
            CanExecuteNextCommand));
    }
}

private void ExecuteNextCommand()
{
    SelectedGameImageIndex += 1;
}

private bool CanExecuteNextCommand() …
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml mvvm mvvm-light

8
推荐指数
1
解决办法
5531
查看次数

在C#控制台应用程序中居中文本仅使用某些输入

我在C#.NET4控制台应用程序中居中文本时遇到问题.

这是我将文本居中的方法:

private static void centerText(String text)
{
    int winWidth = (Console.WindowWidth / 2);
    Console.WriteLine(String.Format("{0,"+winWidth+"}", text));
}
Run Code Online (Sandbox Code Playgroud)

但是,我只是输出正常输出的输出.但是,如果我使用此行:

Console.WriteLine(String.Format("{0,"+winWidth+"}", "text"));
Run Code Online (Sandbox Code Playgroud)

"文本"应该居中.

centerText用这两种方法打电话:

private static void drawStars()
{
    centerText("*********************************************");
}
private static void title(string location)
{
    drawStars();
    centerText("+++ Du er nu her: " + location + "! +++");
    drawStars();
}
Run Code Online (Sandbox Code Playgroud)

c# console-application centering c#-4.0

6
推荐指数
1
解决办法
9054
查看次数

如何在MVC 4应用程序中进行单用户登录

我正在实施一个应用程序,其中使用该应用程序的公司中的每台机器都有一个机制.我正在尝试实现一个用户策略,如果用户处于"Mechanic"角色 - 用户名为Machine1,并且已登录该机器,则只有一个用户可以使用Machine1用户名同时登录公司.如果其他人尝试使用相同的用户名登录,则应阻止该用户名并告知已登录用户.当会话超时到期时,我应该注销登录用户并释放要使用的登录名.当用户自己注销时也会发生同样的情况.我正在尝试在asp.net MVC 4应用程序上构建它.

我曾想过在数据库中使用SessionId,UserId和IsLoggedIn布尔值.但在这种情况下,我需要更改MVC应用程序中会话超时的登录标志以写入数据库,如果许多用户登录,这似乎有点过分.

实施会是什么样的?我应该使用哪些方法或属性来处理数据库中的会话管理?

FYI

我已经制作了自己的方法,我检查用户是否已登录,这里是:

public static bool ValidateUser(string username, string password, string companyName)
{
    int? companyId = myRepository.GetCompanyId(companyName);

    int? userId = companyId == 0 ? null : myRepository.GetUserId(username, companyId);

    if (userId.HasValue && userId.Value != 0)
    {
        var userKey = Security.GenerateUserKey(username, companyName);
        return WebSecurity.Login(userKey, password);
    }
    else
    {
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

在这个方法中,我可以检查会话ID是否与数据库中的相同.

c# asp.net session asp.net-mvc-4 simplemembership

6
推荐指数
1
解决办法
3594
查看次数

使用MVVM处理用户设置

目前我正在WPF使用该MVVM-light框架开发应用程序.

在这一刻,我正在设置我的设置,如我的viewmodel中的下一个示例代码所示:

private string _property

public string Property
{
    get { return _property; }
    set
    {
        if (_property != value)
        {
            _property = value;
            Settings.Default.Property = value;
            RaisePropertyChanged("Property");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在应用程序退出时保存我的设置:

protected override void OnExit(ExitEventArgs e)
{
    Settings.Default.Save();
}
Run Code Online (Sandbox Code Playgroud)

一切都按预期工作,但......

问题:这是一种正确的方法还是有更好的方法来处理MVVM中的设置

c# wpf mvvm mvvm-light

6
推荐指数
1
解决办法
4302
查看次数

Http请求状态码405 ReasonPhrase:''Youtube APi

我尝试发送这两个请求,但我只得到标题中列出的错误作为响应.我对谷歌服务器的两个网络请求是这样的:

HttpClient http = new HttpClient();

HttpResponseMessage response = await http.GetAsync("https://accounts.google.com/o/oauth2/token?code="+localSettings.Values["AccessKey"]+"&client_id=XX-XXXXXXXXXX.apps.googleusercontent.com&client_secret=XXXXX-XXXX&redirect_uri=http://localhost/oauth2callback&grant_type=authorization_code");

//response.EnsureSuccessStatusCode();
Debug.WriteLine(response.ToString());
HttpResponseMessage response1 = await http.GetAsync("https://content.googleapis.com/youtube/v3/subscriptions?part=id&maxResults=10&mine=true&key="+localSettings.Values["AccessKey"]);
Debug.WriteLine(response1.ToString());
Run Code Online (Sandbox Code Playgroud)

我从调试器获得以下输出:

StatusCode: 405, ReasonPhrase: '', Version: 2.0, Content: System.Net.Http.StreamContent, Headers:
{
  server: GSE
  alt-svc: quic=":443"; p="1"; ma=604800
  cache-control: max-age=0, private
  accept-ranges: none
  date: Tue, 29 Sep 2015 16:05:03 GMT
  x-frame-options: SAMEORIGIN
  vary: Accept-Encoding
  x-content-type-options: nosniff
  alternate-protocol: 443:quic,p=1
  x-xss-protection: 1; mode=block
  content-type: application/json
  expires: Tue, 29 Sep 2015 16:05:03 GMT
}
StatusCode: 400, ReasonPhrase: '', Version: 2.0, Content: System.Net.Http.StreamContent, Headers:
{
  server: …
Run Code Online (Sandbox Code Playgroud)

c# api response httprequest http-error

6
推荐指数
1
解决办法
836
查看次数

如何在SendMoney coinbase API中发布数据

我正在尝试使用sendmoney API使用下面的代码发送一些比特币.但我收到下面列出的错误 -

错误:

{"errors":[{"id":"authentication_error","message":"invalid signature"}]}

我正在使用的代码:

string message = time_epoch.data.epoch.ToString() + "POST" + "/v2/accounts/xxxx/transactions"+
        "{type:send,to:xxxx,amount:0.0002,currency:BTC}";

string signature = HashEncode(HashHMAC(StringEncode("xxxxxx"), StringEncode(message)));

var _client = new RestClient("https://api.coinbase.com/v2/");

var request = new RestRequest("accounts/xxxxxx/transactions", Method.POST);

request.AddHeader("CB-VERSION", "2016-10-03");
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Charset", "utf-8");
request.AddHeader("CB-ACCESS-KEY", "xxxxx");
request.AddHeader("CB-ACCESS-SIGN", signature);
request.AddHeader("CB-ACCESS-TIMESTAMP", time_epoch.data.epoch.ToString());
request.AddParameter("type", "send");
request.AddParameter("to", "xxxx");
request.AddParameter("amount", "0.0002");
request.AddParameter("currency", "BTC");
request.AddParameter("idem", "9316dd16-6c09");

request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json;charset=utf-8"; };
var response = _client.Execute(request);
return _client.Execute(request).Content;
Run Code Online (Sandbox Code Playgroud)

c# api bitcoin coinbase-api

5
推荐指数
0
解决办法
469
查看次数

二次公式返回错误

我正在研究一个程序,我正在使用二次公式.这是单击按钮时执行的代码,用于解决问题,

private void button1_Click(object sender, EventArgs e)
{
    double aVal, bVal, cVal, xVal1, xVal2;
    xVal1 = 0;
    xVal2 = 0;
    aVal = Convert.ToDouble(a.Text);
    bVal = Convert.ToDouble(b.Text);
    cVal = Convert.ToDouble(c.Text);
    xVal1 = (bVal + Math.Sqrt(Math.Pow(bVal, 2) - (4 * aVal * cVal))) / 2 * aVal; //Positive Calculation
    xVal2 = (bVal - Math.Sqrt(Math.Pow(bVal, 2) - (4 * aVal * cVal))) / 2 * aVal; //Negative Calculation
    xPos.Text = Convert.ToString(xVal1);
    xNeg.Text = Convert.ToString(xVal2);
}
Run Code Online (Sandbox Code Playgroud)

经过一些调试后,我相信我已将问题缩小到第9行和第10行,实际数学发生在第9行和第10行.但是,我不太确定是什么问题.如您所见,数字是双精度数,并且已初始化,因此它们不为空或截断.当我运行程序并输入say,6,4和3的ab和c值时,负责输出负值和正值的xPos和xNeg标签只显示NaN.我应该使用标签来做这类事吗?

c#

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

仅显示Woocommerce Recent Products短代码中的"库存"产品

在首页上使用Woocommerce Recent Products短代码时,我需要排除显示缺货商品.

[recent_products]
Run Code Online (Sandbox Code Playgroud)

是否有可能创建一个类似hide_outofstock="true"或类似的规则来阻止Out of Stock产品展示?

我已经在网上寻找关于如何处理这个问题的想法,而我根本就没有编码器,但通常我能够坦率地解决这个问题.但是,现在我很难过.所有和任何帮助将不胜感激.

  • 我不能通过WooCommerce设置页面隐藏所有缺货产品,因为它们需要在网站的其他区域中可见.

  • 使用"隐藏"而不是"不拉"缺货产品的代码只显示产品显示的空白区域.

  • 需要在库存水平经常变化时动态工作 - 按产品ID手动限制将花费太长时间.

php wordpress product shortcode woocommerce

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