Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C.结构建议_C#_Visual Studio_Uwp - Fatal编程技术网

C# C.结构建议

C# C.结构建议,c#,visual-studio,uwp,C#,Visual Studio,Uwp,作为一个C初学者,我正在编写一个UWP应用程序。该应用程序目前的工作方式与预期完全相同,但我想知道是否有更好的方法来解决我的图像数组被声明的问题 作为全班同学。我找了几个小时的替代方案,但没有找到一个符合我需要的例子 以下是我的代码的副本: namespace myUWPapp { public sealed partial class MainPage : Page { string[] imgImagesSourceFileName = { "images1.

作为一个C初学者,我正在编写一个UWP应用程序。该应用程序目前的工作方式与预期完全相同,但我想知道是否有更好的方法来解决我的图像数组被声明的问题 作为全班同学。我找了几个小时的替代方案,但没有找到一个符合我需要的例子

以下是我的代码的副本:

namespace myUWPapp
{
    public sealed partial class MainPage : Page
    {
        string[] imgImagesSourceFileName = { "images1.png", "image2.png", "image3.png", "image4.png", "image5.png" };
        string[] btnImagesSourceFileName = { "images6.png", "image7.png", "image8.png", "image9.png", "image10" };

        public MainPage()
        {
            this.InitializeComponent();
            ...
            ...
        }
        private string GetImageFileName(string[] array, int index)
        {
            string image = array[index];

            return image;
        }
        private void ReverseArray(string[] array)
        {
            Array.Reverse(array);
        }
        private void MethodeUsingGetImagesSourceFileName()
        {
            ...         
            string image = GetImageFileName(myArray, myIndex);
            ...         
        }
        private void MethodeUsingReverseArray()
        {
            ...
            ...
            ReversesArray(myImagesArray);
        }
    }
}   

期待其他选择,但别忘了我在编码方面相对较新。

我检查了您的代码,我认为MethodeUsingGetImagesSourceFileName和MethodeUsingReverseArray是不灵活的。若您只想处理字符串数组,可以为图像数组创建ListXTension with List

public static class ListExtension
{
    public static void InitializeImageArray(this List<string> items, int start,int end)
    {
        for (int i = start; i <= end; i++)
        {
            items.Add($"images{i}.png");
        }
    }
    public static string GetImageFileNameWithIndex(this List<string> items, int index)
    {
        return items[index];
    }
}
反向列表


我检查了你的代码,我认为MethodeUsingGetImagesSourceFileName和MethodeUsingReverseArray是不灵活的。若您只想处理字符串数组,可以为图像数组创建ListXTension with List

public static class ListExtension
{
    public static void InitializeImageArray(this List<string> items, int start,int end)
    {
        for (int i = start; i <= end; i++)
        {
            items.Add($"images{i}.png");
        }
    }
    public static string GetImageFileNameWithIndex(this List<string> items, int index)
    {
        return items[index];
    }
}
反向列表


你的照片在哪里?在项目中的文件夹中?如果是这样,您只需执行以下操作即可获得所有图像文件名:

// Get the path to the app's Assets folder.
string root = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;
string path = root + @"\Assets";  // where your image files are located

// Get the folder object that corresponds to this absolute path in the file system.
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(path);

IReadOnlyList<StorageFile> items = await folder.GetFilesAsync();

List<string> listOfNames = items.Select(x=>x.Name).ToList();
//or you can leave it as IEnumerable... or you can convert ToArray()

你的照片在哪里?在项目中的文件夹中?如果是这样,您只需执行以下操作即可获得所有图像文件名:

// Get the path to the app's Assets folder.
string root = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;
string path = root + @"\Assets";  // where your image files are located

// Get the folder object that corresponds to this absolute path in the file system.
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(path);

IReadOnlyList<StorageFile> items = await folder.GetFilesAsync();

List<string> listOfNames = items.Select(x=>x.Name).ToList();
//or you can leave it as IEnumerable... or you can convert ToArray()

你希望你的应用程序做什么?这是我通过创建棋盘游戏学习的第一个应用程序。你可以自由声明一个名为“MyImages”的新类,并将图像数组从主页移动到新类。只需通过公共属性公开它们,以便主页可以使用它们。我没有足够的知识来这样做,因为图像类将只是一个没有内容的结构。然后,从主页上,我需要为每个图像调用一个构造函数。因此,我仍然需要将图像名称放在主目录中。不,我将创建@kennyzx建议的类,并创建一个返回图像的方法,以便所有基于图像的逻辑(显示逻辑除外)都包含在创建的类中。您希望应用程序做什么?对于我通过创建棋盘游戏学习的第一个应用程序。您可以自由声明名为“MyImages”的新类,并将图像数组从MainPage移动到新类。只需通过公共属性公开它们,以便主页可以使用它们。我没有足够的知识来这样做,因为图像类将只是一个没有内容的结构。然后,从主页上,我需要为每个图像调用一个构造函数。因此,我仍然需要将图像名称放在main中。不,我将创建@kennyzx建议的类,并创建一个返回图像的方法,以便所有基于图像的逻辑(显示逻辑除外)都包含在所创建的类中。谢谢,我将尝试此方法并让您知道。这似乎是我一直在寻找的。谢谢。我会试试这个,让你知道。这似乎是我要找的。仅供参考,您应该保留对StorageFile的引用,而不是名称。。。您可以使用Select语句获取用于在getter中显示的名称。仅供参考,您应该只保留对StorageFile的引用,而不是名称。。。您可以使用Select语句获取用于在getter中显示的名称。
// Get the path to the app's Assets folder.
string root = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;
string path = root + @"\Assets";  // where your image files are located

// Get the folder object that corresponds to this absolute path in the file system.
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(path);

IReadOnlyList<StorageFile> items = await folder.GetFilesAsync();

List<string> listOfNames = items.Select(x=>x.Name).ToList();
//or you can leave it as IEnumerable... or you can convert ToArray()