小编jac*_*obz的帖子

使用async在线程中休眠而不会冻结

所以我在这里贴了一个标签("").单击按钮(按钮1)时,标签文本变为"测试".2秒后,文本将重新设置为"".我使用计时器(间隔为2000)完成了这项工作:

private void button1_Click(object sender, EventArgs e)
{
    label1.Text = "Test";
    timer.Enabled = true;
}

private void timer_Tick(object sender, EventArgs e)
{
    label1.Text = "";
}
Run Code Online (Sandbox Code Playgroud)

这有效; 但是,我很好奇它使它在异步方法中工作.

我的代码目前看起来像这样:

private void button1_Click(object sender, EventArgs e)
{
    label1.Text = "Test";
    MyAsyncMethod();
}

public async Task MyAsyncMethod()
{
    await Task.Delay(2000);
    label1.Text = "";
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用.

c# sleep asynchronous freeze

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

Bool列表检查列表中的每个项目是否为false

我有List<bool>很多价值观.检查列表中每个项目是否等于的最有效方法是什么false

c# boolean list

31
推荐指数
3
解决办法
4万
查看次数

在片段中使用TabLayout; 标签文字不可见

我目前正在尝试新的Android支持设计库中的各种新组件.我NavigationView在my中实现了一个MainActivity.java,它使用FragmentManager在Navigation抽屉中的项目之间导航:

getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.content, mTabLayoutFragment)
    .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
    .commit();
Run Code Online (Sandbox Code Playgroud)

TabLayout在其中一个片段中使用了一个.这是片段的布局:

<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"/>

</android.support.design.widget.AppBarLayout>
Run Code Online (Sandbox Code Playgroud)

它背后的Java代码:

import android.os.Bundle;
import android.support.design.widget.*;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TabLayoutFragment extends Fragment {   


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflatedView = inflater.inflate(R.layout.fragment_tablayout, container, false);

        TabLayout tabLayout = (TabLayout) …
Run Code Online (Sandbox Code Playgroud)

android android-fragments android-support-design android-tablayout

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

生成集合的排列 - 有效且有区别

我正在建立代码来自这里.我想生成一组的所有排列,例如(取自线程):

Collection: 1, 2, 3
Permutations: {1, 2, 3}
              {1, 3, 2}
              {2, 1, 3}
              {2, 3, 1}
              {3, 1, 2}
              {3, 2, 1}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述每组的可能排列,但这不是我想要实现的.考虑以下集合:

在此输入图像描述

这会产生 在此输入图像描述 排列,极端的 在此输入图像描述.这需要非常长的时间来计算,因为每个零被认为是唯一的.

而不是那样,我只想产生不同的排列.如果我们这样做,那就是

在此输入图像描述

剩下的排列,因为18项是相同的(k).

现在,我可以运行上述线程中的代码并将结果存储在HashSet中,从而消除了重复的排列.但是,这将是非常低效的.我正在寻找一种算法来直接生成具有区别的排列.

c# algorithm recursion performance permutation

9
推荐指数
1
解决办法
1703
查看次数

Json.NET反序列化对象返回null

我想反序列化字符串o JSON并输出字符串数据:

public class Test
{
    public int id { get; set; }
    public string name { get; set; }
    public long revisionDate { get; set; }
}

private void btnRetrieve_Click(object sender, EventArgs e)
{
    string json = @"{""Name"":{""id"":10,""name"":""Name"",""revisionDate"":1390293827000}}";
    var output = JsonConvert.DeserializeObject<Test>(json);
    lblOutput.Text = output.name;
}
Run Code Online (Sandbox Code Playgroud)

这是为了输出name字符串的属性json.但是,它什么也没有返回.

c# json object json.net

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

使用LINQ获取DataGridView行索引,其中第一列具有特定值

我想得到DataGridViewRow的索引,其第一列的值匹配.

我的代码到目前为止:

string SearchForThis = "test";

int index = from r in dgv.Rows
            where r.Cells[0].Value == SearchForThis
            select r.Index;
Run Code Online (Sandbox Code Playgroud)

编译错误:

找不到源类型'System.Windows.Forms.DataGridViewRowCollection'的查询模式的实现.'哪里'找不到.考虑明确指定范围变量'r'的类型.

c# linq datagridview

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

国家/地区名称为ISO 3166-2代码

我知道如何通过使用将ISO 3166-2代码转换为完整的英文名称,例如"US"到"United States" RegionInfo.

但是,我怎么能做相反的事情,即拿"美国"并返回"美国"?

c# globalization regioninfo cultureinfo iso-3166

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

以编程方式创建CardView不能正确应用样式

我正在尝试创建一个CardView代码.但是,它似乎没有正确应用样式.以下是款式:

<style name="CardViewStyle" parent="CardView">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_margin">8dp</item>
</style>

<style name="Widget.CardContent" parent="android:Widget">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:paddingLeft">16dp</item>
    <item name="android:paddingRight">16dp</item>
    <item name="android:paddingTop">24dp</item>
    <item name="android:paddingBottom">24dp</item>
    <item name="android:orientation">vertical</item>
</style>
Run Code Online (Sandbox Code Playgroud)

在XML中,它看起来像这样:

<android.support.v7.widget.CardView
style="@style/CardViewStyle">
    <LinearLayout
        style="@style/Widget.CardContent">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Title"
            android:textAppearance="@style/TextAppearance.AppCompat.Title" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Caption"/>
    </LinearLayout>
</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)

现在我正在尝试在Java中完全相同:

CardView card = new CardView(new ContextThemeWrapper(MyActivity.this, R.style.CardViewStyle), null, 0);
LinearLayout cardInner = new LinearLayout(new ContextThemeWrapper(MyActivity.this, R.style.Widget_CardContent));

TextView tv_title = new TextView(this);
tv_title.setLayoutParams(new LinearLayout.LayoutParams(
       LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT
));
tv_title.setTextAppearance(this, R.style.TextAppearance_AppCompat_Title);
tv_title.setText("Name");

TextView tv_caption …
Run Code Online (Sandbox Code Playgroud)

android android-styles android-cardview

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

String.Format十进制,包含千位分隔符和强制小数位

我想要String.Format一个小数,以便它有千位分隔符和强制小数位(3).

例如:

输入:

123456,12
78545,8
Run Code Online (Sandbox Code Playgroud)

输出:

123.456,120
78.545,800
Run Code Online (Sandbox Code Playgroud)

我试过了

String.Format("{0:0.0,000}", input);
Run Code Online (Sandbox Code Playgroud)

但这只给出了千位分隔符但不强制小数位.

c# string parsing decimal

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

AES比RSA加密更快?

我试图用ubuntu中的openssl来测试RSA和AES的速度.

我使用以下代码来测试它.

echo -n "0123456789012345" > message.txt

openssl genrsa -out private.pem 1024
openssl rsa -in private.pem -out public.pem -pubout


for i in {1..1000}
do
    openssl rsautl -encrypt -inkey public.pem -pubin -in message.txt -out message_enc.txt
done

for i in {1..1000}
do
    openssl rsautl -decrypt -inkey private.pem -in message_enc.txt -out message_dec.txt
done

for i in {1..1000}
do
    openssl enc -e -aes-128-cbc -in message.txt -out aes.bin -K ddf -iv 345

done
Run Code Online (Sandbox Code Playgroud)

结果:

$ time ./rsa_enc 
real    0m3.697s
user    0m1.308s
sys     0m0.680s

$ time …
Run Code Online (Sandbox Code Playgroud)

encryption performance compare rsa aes

4
推荐指数
2
解决办法
9872
查看次数