如何将数据从回调函数传递到Windows Phone 8中的MainPage(另一个类)

时间:2014-04-10 01:08:31

标签: c# c++ windows-phone-8 windows-runtime

有两个类MainPage和Callback。我使用C ++库,所以我通过CLI获得字节数组。当我获得数据回调类时,我想将数据回调类传递给MainPage类(它是UI线程)。最后,我想在获取字节数组后绘制Image。


<Image x:Name="ImageScreen" x:FieldModifier="public" Height="357" Width="456" Margin="0,33,0,201"/>

== MainPage.xaml.cs ==

public class Callback : ComponentCallback
{
    static byte[] byteArray;        
    ...
    // I get data via this function. When I get the data,
    // I want to pass the data to updateImage function in MainPage class.
    public void onFrameLoaded(int camera, String title, UInt32 bufferSize)
    {
        System.Collections.Generic.List<byte> buffer = MainPage._watcher.getBuffer().ToList();

        byteArray = buffer.ToArray();

        // I want pass byteArray to updateImage.
    }
}    

public partial class MainPage : PhoneApplicationPage
{
    public static Component _watcher;
    public static Callback _watcherCallback;

    public MainPage()
    {
        InitializeComponent();

        CoreComponent _core = new CoreComponent();
        int fenport = _core.fenServerPort();
        _core.setFenServer("", );
        fenport = _core.fenServerPort();

        _watcher = new Component();
        _watcherCallback = new Callback();
        _watcher.setCallback(_watcherCallback);
        _watcher.connect(0, "", 0, "", "");            
    }        

    public void updateImage(byte[] byteArray)
    {
        BitmapImage bi = new BitmapImage();
        MemoryStream stream = new MemoryStream(byteArray);
        bi.SetSource(stream);

        Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
            //Update the UI controls here
            ImageScreen.Source = bi;
        });
    }
}

2 个答案:

答案 0 :(得分:0)

var frame = Window.Current as Frame;

if (frame == null)
{
    return; //?
}

var mainPage = frame.Content as MainPage;

if (mainPage == null)
{
    return; //?
}

mainPage.updateImage(byteArray);

答案 1 :(得分:0)

我解决了这个问题。谢谢你的帮助菲利普。

        Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
            PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame;

            if (frame == null) {
                return; //?
            }

            var mainPage = frame.Content as MainPage;

            if (mainPage == null) {
                return; //?
            }

            mainPage.updateImage(byteArray);
        });