组合框对 Silverlight 中的隐藏代码不可见

发布于 2024-10-28 09:11:14 字数 3528 浏览 2 评论 0原文

我一直在尝试将项目添加到组合框,但似乎无法获取文件后面的代码来识别添加到 xaml 的组合框。我很确定我错过了一些简单的事情。基本上,这里的 xaml 说明了一个空的组合框。后面的代码执行服务,等待 json 返回并反序列化它。不幸的是我无法获取

xaml:

<navigation:Page x:Class="Growing.Views.Room" 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
       mc:Ignorable="d"
       xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
       d:DesignWidth="950" d:DesignHeight="480"
       Title="Home" Style="{StaticResource PageStyle}" DataContext="{Binding}" xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid x:Name="LayoutRoot" ShowGridLines="True" Background="#FF631C00">
    <Grid.ColumnDefinitions>
    </Grid.ColumnDefinitions>
    <Rectangle Height="298" HorizontalAlignment="Left" Margin="195,94,0,0" Name="rect" Stroke="Black" StrokeThickness="2" VerticalAlignment="Top" Width="582" Fill="#FFAAAAAA" RadiusY="0.25" RadiusX="0.25" />
    <sdk:Label Height="38" HorizontalAlignment="Left" Margin="387,160,0,0" Name="label1" VerticalAlignment="Top" Width="203" Content="Select a Room" FontSize="24" FontWeight="Bold" />
    <sdk:Label Height="18" HorizontalAlignment="Left" Margin="312,240,0,0" Name="label2" VerticalAlignment="Top" Width="69" Content="Area:" FontSize="14" />
    <ComboBox x:Name="RoomAreas" Height="23" HorizontalAlignment="Left" Margin="418,235,0,0" VerticalAlignment="Top" Width="209" />
</Grid>

背后的代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Runtime.Serialization.Json;
using System.ServiceModel.Web;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Growing.DataConnectionRef;

namespace Growing.Views
{
    public partial class Room : Page
    {
        public Room()
        {
            InitializeComponent();
            //Asynchronously call the EndReceive Web Service to change the status of an existing open lot record
            WebClient GRService = new WebClient();
            GRService.DownloadStringCompleted += new DownloadStringCompletedEventHandler(GRService_DownloadStringCompleted);
            GRService.DownloadStringAsync(new Uri("/servicestack/GetAreas", UriKind.Relative));
        }

        static void GRService_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            List<Area> dataList = new List<Area>();
            MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(e.Result));
            DataContractJsonSerializer ser = new    DataContractJsonSerializer(dataList.GetType());
            dataList = ser.ReadObject(memoryStream) as List<Area>;
            memoryStream.Close();
            RoomAreas.ItemSource = dataList;

        }

    }
}

在 RoomAreas.ItemSource 我收到错误 非静态字段、方法或属性“Growing.Views.Room.RoomAreas”需要对象引用

抱歉,如果这很难跟随。有人知道这里可能发生什么吗?

先感谢您!

I have been trying to add items to a comboxbox but cannot seem to get the code behind file to recognize the combobox added to the xaml. I am pretty sure I am missing something simple. Basically the xaml here illustrates an empty combobox. The code behind executes the service, waits for json to come back and deserialize it. Unfortunately I cannot get

xaml:

<navigation:Page x:Class="Growing.Views.Room" 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
       mc:Ignorable="d"
       xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
       d:DesignWidth="950" d:DesignHeight="480"
       Title="Home" Style="{StaticResource PageStyle}" DataContext="{Binding}" xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid x:Name="LayoutRoot" ShowGridLines="True" Background="#FF631C00">
    <Grid.ColumnDefinitions>
    </Grid.ColumnDefinitions>
    <Rectangle Height="298" HorizontalAlignment="Left" Margin="195,94,0,0" Name="rect" Stroke="Black" StrokeThickness="2" VerticalAlignment="Top" Width="582" Fill="#FFAAAAAA" RadiusY="0.25" RadiusX="0.25" />
    <sdk:Label Height="38" HorizontalAlignment="Left" Margin="387,160,0,0" Name="label1" VerticalAlignment="Top" Width="203" Content="Select a Room" FontSize="24" FontWeight="Bold" />
    <sdk:Label Height="18" HorizontalAlignment="Left" Margin="312,240,0,0" Name="label2" VerticalAlignment="Top" Width="69" Content="Area:" FontSize="14" />
    <ComboBox x:Name="RoomAreas" Height="23" HorizontalAlignment="Left" Margin="418,235,0,0" VerticalAlignment="Top" Width="209" />
</Grid>

The code behind:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Runtime.Serialization.Json;
using System.ServiceModel.Web;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Growing.DataConnectionRef;

namespace Growing.Views
{
    public partial class Room : Page
    {
        public Room()
        {
            InitializeComponent();
            //Asynchronously call the EndReceive Web Service to change the status of an existing open lot record
            WebClient GRService = new WebClient();
            GRService.DownloadStringCompleted += new DownloadStringCompletedEventHandler(GRService_DownloadStringCompleted);
            GRService.DownloadStringAsync(new Uri("/servicestack/GetAreas", UriKind.Relative));
        }

        static void GRService_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            List<Area> dataList = new List<Area>();
            MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(e.Result));
            DataContractJsonSerializer ser = new    DataContractJsonSerializer(dataList.GetType());
            dataList = ser.ReadObject(memoryStream) as List<Area>;
            memoryStream.Close();
            RoomAreas.ItemSource = dataList;

        }

    }
}

At RoomAreas.ItemSource I get an error An object reference is required for the non-static field, method, or property 'Growing.Views.Room.RoomAreas'

Sorry if this is hard to follow. Anyone have any ideas what might be going on here?

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

场罚期间 2024-11-04 09:11:14

使 GRService_DownloadStringCompleted 方法非静态

void GRService_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            List<Area> dataList = new List<Area>();
            MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(e.Result));
            DataContractJsonSerializer ser = new    DataContractJsonSerializer(dataList.GetType());
            dataList = ser.ReadObject(memoryStream) as List<Area>;
            memoryStream.Close();
            RoomAreas.ItemSource = dataList;

        }

Make GRService_DownloadStringCompleted method non static:

void GRService_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            List<Area> dataList = new List<Area>();
            MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(e.Result));
            DataContractJsonSerializer ser = new    DataContractJsonSerializer(dataList.GetType());
            dataList = ser.ReadObject(memoryStream) as List<Area>;
            memoryStream.Close();
            RoomAreas.ItemSource = dataList;

        }
陪你搞怪i 2024-11-04 09:11:14

尝试将 ItemsSource="{Binding}" 添加到 Xaml 中的组合框属性

Try adding ItemsSource="{Binding}" to the combobox property within the Xaml

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文