簡體   English   中英

有沒有辦法制作一個全局的 C# 字符串?

[英]Is there any way to make a global C# String?

有沒有辦法在C#中共享一個字符串? 該字符串沒有寫在這段代碼中,但我想在公共部分 class MainWindow: Window 和 class 程序之間共享它。 我正在嘗試將“{0} ({1}) {2}”、course.Name、course.Id、course.CourseState 字符串化為字符串名稱 Courses 以放入 Label。謝謝

using Google.Apis.Auth.OAuth2;
using Google.Apis.Classroom.v1;
using Google.Apis.Classroom.v1.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Threading;

namespace Azimuth_Home
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window

    {

        System.Windows.Threading.DispatcherTimer Timer = new System.Windows.Threading.DispatcherTimer();

        public string? MessagIn { get; set; }

        public MainWindow()

        {

            InitializeComponent();

            Timer.Tick += new EventHandler(Timer_Click);

            Timer.Interval = new TimeSpan(0, 0, 1);

            Timer.Start();
            DataContext = this;

        }



        private void Timer_Click(object sender, EventArgs e)

        {

            DateTime d;

            d = DateTime.Now;

            ClockLabelHome.Content = "Local Time: " + d.Hour + " : " + d.Minute + " : " + d.Second + "; " + d.Year + "/" + d.Month + "/" + d.Day;
            ClockLabelHome.FontFamily = new FontFamily("Uni Sans Regular");
            ClockLabelHome.FontSize = 20;

            ClockLabelNews.Content = "Local Time: " + d.Hour + " : " + d.Minute + " : " + d.Second + "; " + d.Year + "/" + d.Month + "/" + d.Day;
            ClockLabelNews.FontFamily = new FontFamily("Uni Sans Regular");
            ClockLabelNews.FontSize = 20;

            ClockLabelWork.Content = "Local Time: " + d.Hour + " : " + d.Minute + " : " + d.Second + "; " + d.Year + "/" + d.Month + "/" + d.Day;
            ClockLabelWork.FontFamily = new FontFamily("Uni Sans Regular");
            ClockLabelWork.FontSize = 20;

            ClockLabelChat.Content = "Local Time: " + d.Hour + " : " + d.Minute + " : " + d.Second + "; " + d.Year + "/" + d.Month + "/" + d.Day;
            ClockLabelChat.FontFamily = new FontFamily("Uni Sans Regular");
            ClockLabelChat.FontSize = 20;

            if (d.Minute < 10)
            {
                ClockLabelHome.Content = "Local Time: " + d.Hour + " : 0" + d.Minute + " : " + d.Second + "; " + d.Year + "/" + d.Month + "/" + d.Day;
                ClockLabelNews.Content = "Local Time: " + d.Hour + " : 0" + d.Minute + " : " + d.Second + "; " + d.Year + "/" + d.Month + "/" + d.Day;
                ClockLabelWork.Content = "Local Time: " + d.Hour + " : 0" + d.Minute + " : " + d.Second + "; " + d.Year + "/" + d.Month + "/" + d.Day;
                ClockLabelChat.Content = "Local Time: " + d.Hour + " : 0" + d.Minute + " : " + d.Second + "; " + d.Year + "/" + d.Month + "/" + d.Day;
            }
            if (d.Second < 10)
            {
                ClockLabelHome.Content = "Local Time: " + d.Hour + " : " + d.Minute + " : 0" + d.Second + "; " + d.Year + "/" + d.Month + "/" + d.Day;
                ClockLabelNews.Content = "Local Time: " + d.Hour + " : " + d.Minute + " : 0" + d.Second + "; " + d.Year + "/" + d.Month + "/" + d.Day;
                ClockLabelWork.Content = "Local Time: " + d.Hour + " : " + d.Minute + " : 0" + d.Second + "; " + d.Year + "/" + d.Month + "/" + d.Day;
                ClockLabelChat.Content = "Local Time: " + d.Hour + " : " + d.Minute + " : 0" + d.Second + "; " + d.Year + "/" + d.Month + "/" + d.Day;
            }

        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(MessagIn);
        }
        private void OnKeyDownHandler(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                MessageInput.Text = "You Entered: " + MessageInput.Text;
            }
        }


    }
    class Program
    {
        // If modifying these scopes, delete your previously saved credentials
        // at ~/.credentials/classroom.googleapis.com-dotnet-quickstart.json
        static string[] Scopes = { ClassroomService.Scope.ClassroomCoursesReadonly };
        static string ApplicationName = "Classroom API .NET Quickstart";

        private static void main(string[] args)
        {
            UserCredential credential;

            using (var stream =
                new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
            {
                // The file token.json stores the user's access and refresh tokens, and is created
                // automatically when the authorization flow completes for the first time.
                string credPath = "token.json";
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.FromStream(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
                Console.WriteLine("Credential file saved to: " + credPath);
            }

            // Create Classroom API service.
            var service = new ClassroomService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });

            // Define request parameters.
            CoursesResource.ListRequest request = service.Courses.List();
            request.PageSize = 10;

            // List courses.
            ListCoursesResponse response = request.Execute();
            Console.WriteLine("Courses:");
            if (response.Courses != null && response.Courses.Count > 0)
            {
                foreach (var course in response.Courses)
                {
                    Console.WriteLine("{0} ({1}) {2}", course.Name, course.Id, course.CourseState);
                    
                }
            }
            else
            {
                Console.WriteLine("No courses found.");
            }
            Console.Read();

        }
    }
}

非常感謝你。

顯然,每次您想“打印課程”時,您都需要相同的 Name、Id 和 CourseState 組合。 這樣做的標准方法是重寫 ToString 方法:

public class Course
{
   //...
  
   public override string ToString()
   {
       return String. Format("{0} ({1}) {2}", Name, Id, CourseState);
   }
}

如果您然后Console.WriteLine(course) ,默認情況下會調用該 ToString 方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM