Windows phone 7 如何在windows phone 7中更新列表中的内部控件值?

Windows phone 7 如何在windows phone 7中更新列表中的内部控件值?,windows-phone-7,windows-phone,Windows Phone 7,Windows Phone,我在windows phone应用程序中工作,其中我有一个ListBox控件。和一个按钮作为内部控制 所以在列表中20个按钮后绑定数据。现在我想更改按钮单击后的数据 这是我的示例代码。这是我在windows phone上的第一个应用程序 <ListBox Grid.Row="1" HorizontalAlignment="Left" Margin="10,10,0,0" Name="lstInstagramTags" VerticalAlignment="Top" Width="444"

我在windows phone应用程序中工作,其中我有一个ListBox控件。和一个按钮作为内部控制

所以在列表中20个按钮后绑定数据。现在我想更改按钮单击后的数据

这是我的示例代码。这是我在windows phone上的第一个应用程序

 <ListBox Grid.Row="1" HorizontalAlignment="Left" Margin="10,10,0,0" Name="lstInstagramTags" VerticalAlignment="Top" Width="444" Height="562" SelectionChanged="lstInstagramTags_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Height="100" Margin="-10,-10,-10,-10">

                        <Button Click="Button_Click" Content="NeedToChangeThisValue" Width="150" FontSize="13" Height="60" Margin="-560,35,5,5" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

    private void Button_Click(object sender, RoutedEventArgs e)
            {
    HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(myUri);
                    myRequest.Method = "POST";
                    myRequest.ContentType = "application/x-www-form-urlencoded";
                    myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest);
    }

void GetRequestStreamCallback(IAsyncResult callbackResult)
        {
            HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
            // End the stream request operation
            Stream postStream = myRequest.EndGetRequestStream(callbackResult);

            string PostData = "action=follow&access_token=966258514.201df4f.4b1c5015a7784a63aac00b9a902c4176";

            // Create the post data
            string postData = PostData;
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            // Add the post data to the web request
            postStream.Write(byteArray, 0, byteArray.Length);
            postStream.Close();

            // Start the web request
            myRequest.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), myRequest);
        }

void GetResponsetStreamCallback(IAsyncResult callbackResult)
        {
            HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
            using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
            {
                **string result** = httpWebStreamReader.ReadToEnd();

            }
        }

私有无效按钮\u单击(对象发送者,路由目标e)
{
HttpWebRequest myRequest=(HttpWebRequest)HttpWebRequest.Create(myUri);
myRequest.Method=“POST”;
myRequest.ContentType=“application/x-www-form-urlencoded”;
BeginGetRequestStream(新的异步回调(GetRequestStreamCallback),myRequest);
}
void GetRequestStreamCallback(IAsyncResult callbackResult)
{
HttpWebRequest myRequest=(HttpWebRequest)callbackResult.AsyncState;
//结束流请求操作
Stream postStream=myRequest.EndGetRequestStream(callbackResult);
string PostData=“action=follow&access_token=966258514.201df4f.4b1c5015a7784a63aac00b9a902c4176”;
//创建post数据
字符串postData=postData;
byte[]byteArray=Encoding.UTF8.GetBytes(postData);
//将post数据添加到web请求
Write(byteArray,0,byteArray.Length);
postStream.Close();
//启动web请求
BeginGetResponse(新的AsyncCallback(GetResponsetStreamCallback),myRequest);
}
void GetResponseStreamCallback(IAsyncResult callbackResult)
{
HttpWebRequest请求=(HttpWebRequest)callbackResult.AsyncState;
HttpWebResponse=(HttpWebResponse)request.EndGetResponse(callbackResult);
使用(StreamReader httpWebStreamReader=newstreamreader(response.GetResponseStream())
{
**字符串结果**=httpWebStreamReader.ReadToEnd();
}
}
我想根据结果更改按钮数据(标记为粗体)。我没有办法根据这个结果更新任何控件

请给我推荐一个好办法


谢谢

您需要使用可视化树帮助器来实现相同的功能

使用此方法在列表框的可视树中进行挖掘

 public static T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
        {
            try
            {
                int childCount = VisualTreeHelper.GetChildrenCount(parentElement);
                if (childCount == 0)
                    return null;

                for (int i = 0; i < childCount; i++)
                {
                    var child = VisualTreeHelper.GetChild(parentElement, i);
                    if (child != null && child is T)
                    {
                        return (T)child;
                    }
                    else
                    {
                        var result = FindFirstElementInVisualTree<T>(child);
                        if (result != null)
                            return result;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return null;
        }
public static T findfirstelementvisualtree(DependencyObject parentElement),其中T:DependencyObject
{
尝试
{
int childCount=visualtreeheloper.GetChildrenCount(parentElement);
if(childCount==0)
返回null;
for(int i=0;i
这就是在代码中使用这个方法的方式

ListBoxItem SelectedListBoxItem = this.lstInstagramTags.ItemContainerGenerator.ContainerFromIndex(int index) as ListBoxItem;
                if (SelectedListBoxItem == null)
                    return;
                // Iterate whole listbox tree and search for this items
                Button btn= common.FindFirstElementInVisualTree<Button>(SelectedListBoxItem );
                btn.Content="Hello";
ListBoxItem SelectedListBoxItem=this.lstInstagramTags.ItemContainerGenerator.ContainerFromIndex(int-index)作为ListBoxItem;
如果(SelectedListBoxItem==null)
返回;
//迭代整个列表框树并搜索此项
按钮btn=common.findFirstElementVisualTree(SelectedListBoxItem);
btn.Content=“你好”;
还有一个


希望这有帮助。

我认为这个问题主要是基于选项的,所以这里有三个想法

  • 球状按钮
在当前页面中定义按钮

private Button helerButton;

   private void Button_Click(object sender, RoutedEventArgs e)
        {
            helerButton = (Button)sender;
            HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(myUri);
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest);
        }

void GetResponsetStreamCallback(IAsyncResult callbackResult)
        {
            HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
            using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
            {
               string result = httpWebStreamReader.ReadToEnd();
               helerButton.Content = result

            }
        }
  • 第二种方法
您可以稍微更改代码体系结构

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(myUri);
        myRequest.Method = "POST";
        myRequest.ContentType = "application/x-www-form-urlencoded";
        myRequest.BeginGetRequestStream(r => 
        {
            var httpRequest = (HttpWebRequest)r.AsyncState;
            var httpResponse = (HttpWebResponse)httpRequest.EndGetResponse(r);
            Stream postStream = myRequest.EndGetRequestStream(r);
            string PostData = "action=follow&access_token=966258514.201df4f.4b1c5015a7784a63aac00b9a902c4176";           
            string postData = PostData;
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            postStream.Write(byteArray, 0, byteArray.Length);
            postStream.Close();
            httpRequest.BeginGetResponse(q => 
            {
                    HttpWebRequest request = (HttpWebRequest)q.AsyncState;
                    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(q);
                    using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
                    {
                        string result = httpWebStreamReader.ReadToEnd();

                        Dispatcher.BeginInvoke(() =>
                        {
                            Button b = (Button)sender;
                            b.Content = result;
                            //for consideration 
                            //b.IsEnabled 

                        });
                    }
            }, httpRequest);

        }, myRequest);
    }
  • 最终方法(我个人将选择此方法)
就是创建任务