小编Jar*_*ice的帖子

如何将变量保存到新的文本文件,以便下次运行程序时加载这些变量?

新 C#er 在这里。我正在制作基于控制台的 RPG。进展顺利,但我需要找出如何保存游戏。我猜想有一种方法可以将我的应用程序中的变量保存到文本文件中,该文件可用于在应用程序再次运行时加载变量。不幸的是,我不知道从哪里开始。

另外,我需要一种在加载保存文件时转到代码中某个点的方法。

我的一些变量包括:

int xCoordinate, yCoordinate, hp, hpmax, level;
Run Code Online (Sandbox Code Playgroud)

任何示例代码将不胜感激。

c# console save

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

有没有办法缩短我的代码?

新的C#呃这里.我正在通过控制台应用程序进行基于文本的冒险.我做了一个地图命令,当你输入"地图"时,它会显示一个地图,其中一个X表示你当前的位置.Int变量xCoordinate和yCoordinate用于表示地图上的字符位置,每当您键入"go north"或"go south"等时,它将更改为1.地图为13x10,因此您的角色有130个可能的位置.我做了130个不同的if语句,它运行得很好.我的问题是,是否有更有效/更简单的方法来做到这一点.这是我的代码的样子:

    public static void Map()

    {
        Console.ForegroundColor = ConsoleColor.Cyan;
        Console.WriteLine("\n" + xCoordinate + ", " + yCoordinate);
        Console.WriteLine("\nTowns are represented by a \"T\". Current location is shown as an \"X\".\n");
        Console.ForegroundColor = ConsoleColor.DarkGray;
        if ((xCoordinate == -7) && (yCoordinate == -4))
        {
            Console.Write("[ ][ ][ ][ ][ ][ ][ ][T][ ][ ][ ][ ][ ] 5\n");
            Console.Write("[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ] 4\n");
            Console.Write("[ ][ ][ ][ ][T][ ][ ][ …
Run Code Online (Sandbox Code Playgroud)

c# console

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

问:如何更改特定QComboBox项目的突出显示颜色

我正在努力使QComboBox的高亮显示透明.此QComboBox的颜色也会根据所选索引而更改.这是迄今为止我最好的解决方案:

switch(comboBox->currentIndex())
{
case 0:
    comboBox->setStyleSheet("QWidget {color:black}");
    break;

case 1:
    comboBox->setStyleSheet("QWidget {background-color:red; color:white;}");
    break;

case 2:
    comboBox->setStyleSheet("QWidget {background-color:green; color:white;}");
    break;
}


comboBox->setItemData(0, QColor(Qt::white), Qt::BackgroundRole);
comboBox->setItemData(0, QColor(Qt::black), Qt::ForegroundRole);
comboBox->setItemData(1, QColor(Qt::red), Qt::BackgroundRole);
comboBox->setItemData(1, QColor(Qt::white), Qt::ForegroundRole);
comboBox->setItemData(2, QColor(Qt::darkGreen), Qt::BackgroundRole);
comboBox->setItemData(2, QColor(Qt::white), Qt::ForegroundRole);

QPalette p = comboBox->palette();
p.setColor(QPalette::Highlight, Qt::transparent);
comboBox->setPalette(p);

p = comboBox->view()->palette();
p.setColor(QPalette::Highlight, Qt::transparent);

comboBox->view()->setPalette(p);
Run Code Online (Sandbox Code Playgroud)

问题是QComboBox当前的颜色是弹出窗口中选择项目时的高亮颜色.我希望每个QComboBox项目保持相同的颜色.图像显示了我遇到的问题.

在此输入图像描述 在此输入图像描述 在此输入图像描述

qt qcombobox

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

Qt检查QString以查看它是否是有效的十六进制值

我正在和一个现有项目的Qt合作.我正在尝试通过串行电缆将字符串发送到恒温器以向其发送命令.我需要确保字符串只包含0-9,af,并且长度不超过或少于6个字符.我试图使用QString.contains,但我现在卡住了.任何帮助,将不胜感激.

qstring qt hex

3
推荐指数
1
解决办法
4841
查看次数

Android SlidingUpPanelLayout上滑事件

我正在使用https://github.com/umano/AndroidSlidingUpPanel.它工作得很好,但我想找出一些方法来监听滑动上/下事件.我在README中没有看到任何与此相关的内容.

file.xml:

<com.sothree.slidinguppanel.SlidingUpPanelLayout
   xmlns:sothree="http://schemas.android.com/apk/res-auto"
   android:id="@+id/sliding_layout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:gravity="bottom"
   sothree:fadeColor="@android:color/transparent"
   sothree:panelHeight="30dp"
   sothree:shadowHeight="4dp">

    <RelativeLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin" >

        <!-- always visible content -->

    </RelativeLayout>


    <RelativeLayout android:id="@+id/slider"
        android:layout_width="match_parent"
        android:layout_height="250dp" >

        <!-- slide-able stuff -->

        <ImageView
            android:id="@+id/sliderImage"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:src="@drawable/slide_up"
            android:gravity="center|top" />

        <ListView
            android:id="@+id/table"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/sliderImage" >

        </ListView>

    </RelativeLayout>

</com.sothree.slidinguppanel.SlidingUpPanelLayout>
Run Code Online (Sandbox Code Playgroud)

someJava.java:

SlidingUpPanelLayout slider = (SlidingUpPanelLayout)findViewById(R.id.sliding_layout);
ImageView sliderImage = (ImageView)findViewById(R.id.sliderImage);

/* slider.setOnSomethingListenerIdk(new SomeSortOfOnSomethingListener(){
    @Override
    public boolean onSomething(View view, SomeEvent someEvent) {
         if (view.isUpOrSomething) sliderImage.setImageResource(R.drawable.slide_down);
         else sliderImage.setImageResource(R.drawable.slide_up); …
Run Code Online (Sandbox Code Playgroud)

java events android panel slider

3
推荐指数
1
解决办法
5062
查看次数

Xamarin Android - BroadcastReceiver问题 - 无法实例化接收器

我的应用程序使用Geofencing Service在用户进入和退出Geofence时向API发送http请求.我需要Service停止或启动它,具体取决于用户是否具有GPS访问权限.在应用程序完全关闭后,我还需要这样做,因为禁用位置服务会使后台服务崩溃.

问题是每当我启用或禁用位置服务时,应用程序都会崩溃并显示此堆栈跟踪:

java.lang.RuntimeException: Unable to instantiate receiver com.MyCompany.MyApp.GeofenceBroadcastReceiver: java.lang.ClassNotFoundException: Didn't find class "com.MyCompany.MyApp.GeofenceBroadcastReceiver" on path: DexPathList[[zip file "/data/app/com.MyCompany.MyApp-1/base.apk"],nativeLibraryDirectories=[/data/app/com.MyCompany.MyApp-1/lib/arm, /vendor/lib, /system/lib]]
Run Code Online (Sandbox Code Playgroud)

有趣的是,这仅在更改位置服务时发生,而不是在更改飞行模式时发生.在任何一种情况下OnReceiveBroadcastReceiver都被称为.

我手动编辑了我的AndroidManifest.xml文件.不确定这可能与它有关.根据这个可能.

AndroidManifest.xml中

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.MyCompany.MyApp" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="19" />
    <application android:label="MyApp" android:icon="@drawable/Icon">
        <meta-data android:name="come.google.android.maps.v2.API_KEY" android:value="SomeKeyValueForGoogleAPI" />
        <receiver android:name=".GeofenceBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.ACTION_AIRPLANE_MODE_CHANGED"></action>
                <action android:name="android.intent.action.ACTION_BOOT_COMPLETED"></action>
                <action android:name="android.location.PROVIDERS_CHANGED" />
            </intent-filter>
        </receiver>
    </application>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.LOCATION_HARDWARE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION" …
Run Code Online (Sandbox Code Playgroud)

c# android broadcastreceiver classnotfoundexception xamarin

3
推荐指数
1
解决办法
3171
查看次数

C#类转换为类

假设我有两对具有完全相同属性的两个不同类.

public class Class1
{
    public int IntValue { get; set; }
    public string StringValue { get; set; }
}

public class Class2
{
    public int IntValue { get; set; }
    public string StringValue { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

是否有可能创建一个方法,将任何object作为参数并返回object我传入的位置,Class1并返回Class2或反之亦然,而不知道将传入哪个类?我们可以假设这两个类将共享完全相同的属性.我们需要定义在Type调用方法时返回的内容.

c#

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

无法捕获从任务抛出的异常

我对此已经摸不着头脑有一段时间了。我不知道这个异常是在哪里抛出的。我到底应该在哪里捕获这个异常?它是System.Net.WebException。我想我只是没有得到任务和异步/等待。我知道我正在创建其他线程,这很好,但是我在哪里处理异常?

The remote server returned an error: (403) Forbidden.

这是代码:

public async void sendRequest(string database, string table, string where, bool isPost, int deviceType)
{
    string urlQuery = Constants.URL_QUERY + database + "/" + table + "?" + where;

    Debug.WriteLine("URL Query: " + urlQuery);

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlQuery);
    request.Method = isPost ? "POST" : "GET";
    request.ContentType = "application/json";

    try
    {
        Debug.WriteLine("getting response");
        WebResponse response = await makeAsyncRequest(request);
        Debug.WriteLine("response obtained");
        finishRequest(response);
    }
    catch (WebException e)
    {
        Debug.WriteLine("WebException caught");
    }
} …
Run Code Online (Sandbox Code Playgroud)

c# task webexception async-await

0
推荐指数
1
解决办法
1516
查看次数