Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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# 来自JSON的幻灯片图像_C#_Json_Image_Uwp_Slideshow - Fatal编程技术网

C# 来自JSON的幻灯片图像

C# 来自JSON的幻灯片图像,c#,json,image,uwp,slideshow,C#,Json,Image,Uwp,Slideshow,我想创建一个图像幻灯片。图片取自json上的数据(如下所示)。 我尝试使用以下代码: DispatcherTimer playlistTimer1a = null; List<string> Images1a = new List<string>(); protected override void OnNavigatedTo(NavigationEventArgs e) { ImageSource1a(); } private async void Imag

我想创建一个图像幻灯片。图片取自json上的数据(如下所示)。 我尝试使用以下代码:

DispatcherTimer playlistTimer1a = null;
List<string> Images1a = new List<string>();

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    ImageSource1a();
}

private async void ImageSource1a()
        {
            try
            {
                var httpClientHandler = new HttpClientHandler();
                httpClientHandler.Credentials = new System.Net.NetworkCredential("username", "password");
                var httpClient = new HttpClient(httpClientHandler);
                string urlPath = "http://";
                var values = new List<KeyValuePair<string, string>>
                {
                    new KeyValuePair<string, string>("platform","win"),     
                };
                HttpResponseMessage response = await httpClient.PostAsync(urlPath, new FormUrlEncodedContent(values));

                response.EnsureSuccessStatusCode();

                string jsonText = await response.Content.ReadAsStringAsync();
                JsonObject jsonObject = JsonObject.Parse(jsonText);
                //JsonObject jsonData1 = jsonObject["data"].GetObject();

                JsonArray jsonData1 = jsonObject["data"].GetArray();

                foreach (JsonValue groupValue1 in jsonData1)
                {

                    JsonObject groupObject1 = groupValue1.GetObject();

                    string image = groupObject1["image"].GetString();
                    string url = groupObject1["url"].GetString();

                    Banner file1 = new Banner();
                    file1.Image = image;
                    file1.URL = url;
                    Images1a.Add(file1.Image);
                    playlistTimer1a = new DispatcherTimer();
                    playlistTimer1a.Interval = new TimeSpan(0, 0, 6);
                    playlistTimer1a.Tick += playlistTimer_Tick1a;
                    topBanner.Source = new BitmapImage(new Uri(file1.Image));
                    playlistTimer1a.Start();
                }
            }
            catch (HttpRequestException ex)
            {
                RequestException();
            }
        }

 int count1a = 0;

void playlistTimer_Tick1a(object sender, object e)
        {
            if (Images1a != null)
            {
                if (count1a < Images1a.Count)
                    count1a++;

                if (count1a >= Images1a.Count)
                    count1a = 0;

                ImageRotation1a();
            }
        }

private async void ImageRotation1a()
        {
            OpacityTrans1.Begin();
        }
dispatchermer playtimer1a=null;
列表图像1A=新列表();
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
ImageSource1a();
}
专用异步void ImageSource1a()
{
尝试
{
var httpClientHandler=新的httpClientHandler();
httpClientHandler.Credentials=新系统.Net.NetworkCredential(“用户名”、“密码”);
var httpClient=新的httpClient(httpClientHandler);
字符串urlPath=“http://”;
var值=新列表
{
新的KeyValuePair(“平台”、“赢”),
};
HttpResponseMessage response=等待httpClient.PostAsync(urlPath,新表单URLEncodedContent(值));
response.EnsureSuccessStatusCode();
string jsonText=await response.Content.ReadAsStringAsync();
JsonObject-JsonObject=JsonObject.Parse(jsonText);
//JsonObject jsonData1=JsonObject[“数据”].GetObject();
JsonArray jsonData1=jsonObject[“数据”].GetArray();
foreach(jsonData1中的JsonValue groupValue1)
{
JsonObject groupObject1=groupValue1.GetObject();
string image=groupObject1[“image”].GetString();
字符串url=groupObject1[“url”].GetString();
横幅文件1=新横幅();
file1.Image=Image;
file1.URL=URL;
Images1a.Add(file1.Image);
PlayTimer1A=新调度程序();
PlayTimer1A.Interval=新的时间跨度(0,0,6);
playltimer1A.Tick+=playltimer_Tick1a;
topBanner.Source=新的位图图像(新的Uri(file1.Image));
playlistimer1a.Start();
}
}
捕获(HttpRequestException-ex)
{
请求异常();
}
}
int count1a=0;
void playlimer_Tick1a(对象发送器,对象e)
{
如果(图像1A!=null)
{
if(计数1a<图像1a.计数)
count1a++;
如果(count1a>=图像1a.计数)
count1a=0;
图像旋转1a();
}
}
私有异步void ImageRotation1a()
{
OpacityTrans1.Begin();
}
而问题只是一张幻灯片显示的图像索引仅为0,无法更改图片。我怎样才能克服这个问题

而问题只是一张幻灯片显示的图像索引仅为0,无法更改图片

foreach
循环会分解所有内容。如果要制作幻灯片图像,请不要在
foreach
循环中包含
dispatchermer
,您可以通过以下步骤修改代码:

  • 修改
    ImageSource1a
    方法,如下所示:

    private async void ImageSource1a()
    {
        try
        {
            ...
            foreach (JsonValue groupValue1 in jsonData1)
            {
                JsonObject groupObject1 = groupValue1.GetObject();
                string image = groupObject1["image"].GetString();
                string url = groupObject1["url"].GetString();
                Images1a.Add(image);
            }
               //I don't know what is Banner object used for but for this piece of codes, a banner object is not necessary.
                //Banner file1 = new Banner();
                //file1.Image = image;
                //file1.URL = url;
    
                playlistTimer1a = new DispatcherTimer();
                playlistTimer1a.Interval = new TimeSpan(0, 0, 6);
                playlistTimer1a.Tick += playlistTimer_Tick1a;
                topBanner.Source = new BitmapImage(new Uri(Images1a[0]));//set the current image to the first one
                playlistTimer1a.Start();
        }
        catch (HttpRequestException ex)
        {
            RequestException();
        }
    }
    
    private void playlistTimer_Tick1a(object sender, object e)
    {
        if (Images1a != null)
        {
    
            if (count1a < Images1a.Count)
                count1a++;
    
            if (count1a >= Images1a.Count)
                count1a = 0;
            topBanner.Source = new BitmapImage(new Uri(Images1a[count1a]));
            ImageRotation1a();
        }
    }
    
  • 修改您的
    播放计时器\u Tick1a
    方法,如下所示:

    private async void ImageSource1a()
    {
        try
        {
            ...
            foreach (JsonValue groupValue1 in jsonData1)
            {
                JsonObject groupObject1 = groupValue1.GetObject();
                string image = groupObject1["image"].GetString();
                string url = groupObject1["url"].GetString();
                Images1a.Add(image);
            }
               //I don't know what is Banner object used for but for this piece of codes, a banner object is not necessary.
                //Banner file1 = new Banner();
                //file1.Image = image;
                //file1.URL = url;
    
                playlistTimer1a = new DispatcherTimer();
                playlistTimer1a.Interval = new TimeSpan(0, 0, 6);
                playlistTimer1a.Tick += playlistTimer_Tick1a;
                topBanner.Source = new BitmapImage(new Uri(Images1a[0]));//set the current image to the first one
                playlistTimer1a.Start();
        }
        catch (HttpRequestException ex)
        {
            RequestException();
        }
    }
    
    private void playlistTimer_Tick1a(object sender, object e)
    {
        if (Images1a != null)
        {
    
            if (count1a < Images1a.Count)
                count1a++;
    
            if (count1a >= Images1a.Count)
                count1a = 0;
            topBanner.Source = new BitmapImage(new Uri(Images1a[count1a]));
            ImageRotation1a();
        }
    }
    
    private void播放计时器\u Tick1a(对象发送方,对象e)
    {
    如果(图像1A!=null)
    {
    if(计数1a<图像1a.计数)
    count1a++;
    如果(count1a>=图像1a.计数)
    count1a=0;
    Source=新的位图图像(新的Uri(Images1a[count1a]);
    图像旋转1a();
    }
    }
    
  • 更新:这是我的基本演示:

    而问题只是一张幻灯片显示的图像索引仅为0,无法更改图片

    foreach
    循环会分解所有内容。如果要制作幻灯片图像,请不要在
    foreach
    循环中包含
    dispatchermer
    ,您可以通过以下步骤修改代码:

  • 修改
    ImageSource1a
    方法,如下所示:

    private async void ImageSource1a()
    {
        try
        {
            ...
            foreach (JsonValue groupValue1 in jsonData1)
            {
                JsonObject groupObject1 = groupValue1.GetObject();
                string image = groupObject1["image"].GetString();
                string url = groupObject1["url"].GetString();
                Images1a.Add(image);
            }
               //I don't know what is Banner object used for but for this piece of codes, a banner object is not necessary.
                //Banner file1 = new Banner();
                //file1.Image = image;
                //file1.URL = url;
    
                playlistTimer1a = new DispatcherTimer();
                playlistTimer1a.Interval = new TimeSpan(0, 0, 6);
                playlistTimer1a.Tick += playlistTimer_Tick1a;
                topBanner.Source = new BitmapImage(new Uri(Images1a[0]));//set the current image to the first one
                playlistTimer1a.Start();
        }
        catch (HttpRequestException ex)
        {
            RequestException();
        }
    }
    
    private void playlistTimer_Tick1a(object sender, object e)
    {
        if (Images1a != null)
        {
    
            if (count1a < Images1a.Count)
                count1a++;
    
            if (count1a >= Images1a.Count)
                count1a = 0;
            topBanner.Source = new BitmapImage(new Uri(Images1a[count1a]));
            ImageRotation1a();
        }
    }
    
  • 修改您的
    播放计时器\u Tick1a
    方法,如下所示:

    private async void ImageSource1a()
    {
        try
        {
            ...
            foreach (JsonValue groupValue1 in jsonData1)
            {
                JsonObject groupObject1 = groupValue1.GetObject();
                string image = groupObject1["image"].GetString();
                string url = groupObject1["url"].GetString();
                Images1a.Add(image);
            }
               //I don't know what is Banner object used for but for this piece of codes, a banner object is not necessary.
                //Banner file1 = new Banner();
                //file1.Image = image;
                //file1.URL = url;
    
                playlistTimer1a = new DispatcherTimer();
                playlistTimer1a.Interval = new TimeSpan(0, 0, 6);
                playlistTimer1a.Tick += playlistTimer_Tick1a;
                topBanner.Source = new BitmapImage(new Uri(Images1a[0]));//set the current image to the first one
                playlistTimer1a.Start();
        }
        catch (HttpRequestException ex)
        {
            RequestException();
        }
    }
    
    private void playlistTimer_Tick1a(object sender, object e)
    {
        if (Images1a != null)
        {
    
            if (count1a < Images1a.Count)
                count1a++;
    
            if (count1a >= Images1a.Count)
                count1a = 0;
            topBanner.Source = new BitmapImage(new Uri(Images1a[count1a]));
            ImageRotation1a();
        }
    }
    
    private void播放计时器\u Tick1a(对象发送方,对象e)
    {
    如果(图像1A!=null)
    {
    if(计数1a<图像1a.计数)
    count1a++;
    如果(count1a>=图像1a.计数)
    count1a=0;
    Source=新的位图图像(新的Uri(Images1a[count1a]);
    图像旋转1a();
    }
    }
    

  • 更新:这是我的基本演示:。

    我建议您使用此库处理json,并使用此实用程序从json创建c#类。我的问题是无法从json生成幻灯片图像(只能拍摄json中的第一张图片,因此图片不会更改,只会闪烁)。我像上面的代码一样使用我建议您使用这个库来处理json,这个实用程序从json创建c#类我的问题是无法从json生成幻灯片图像(只能拍摄json中的第一张图片,因此图片不会改变,只会闪烁)。当我尝试使用代码替换现有方法ImageSource1a时,我使用了上面的代码错误,如下图:如何处理?抱歉,我忘记注释一些代码,我已更新了我的答案。我尝试了上面方法ImageSource1a的代码,但幻灯片只会单独闪烁,图片不会更改(仅限第一幅图像)。如何处理?您还需要更新播放计时器\u Tick1a方法的代码。我在此方法中添加了
    topBanner.Source=新位图图像(新Uri(Images1a[count1a]))
    。我已更新播放计时器\u Tick1a方法的代码。我添加了topBanner.Source=新位图图像(新Uri(Images1a[count1a])))在这种方法中。但是,幻灯片只会单独闪烁,图片不会更改(仅第一张图片)。如何处理?当我尝试使用代码替换现有方法ImageSource1a时,错误如下:如何处理?抱歉,我忘记了