2016-12-01 19 views
0

我正在使用xamarin開發一個android應用程序。我做了標籤頁。對於第二頁,我想從我的android相機中顯示camerastream。爲此,一些示例代碼表示我需要在應用的android部分中使用textureView,但是該紋理視圖需要放在第二頁上。每當我嘗試訪問該頁面內的Stacklayout時,出現以下錯誤:由於其保護級別,'Page1.camera'無法訪問。 使用x:FieldModifier =「public」該堆棧佈局內部也不起作用。從android項目中的xamarin.forms到達一個頁面

這裏是我的代碼的結構,使之更加明確

在這裏,我做了標籤頁:

MainPage = new TabbedPage 
     { 
      Children = { 
       new MainPage(), 
       new Page1(), 
       new Page2() 

      } 
     }; 

內部的第一頁,我有這樣的:

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
     x:Class="App4.Page1" 
     Title="Licht"> 
<StackLayout x:Name="camera" x:FieldModifier="public" Orientation="Vertical"> 
</StackLayout> 
</ContentPage> 

而且裏面MainActivity.cs我有這個地方我必須訪問相機。

_textureView = new TextureView(Page1.camera); 

這是我的應用程序 And this is the structure of my app

回答

0

使用X的結構:FieldModifier = 「公共」 裏面那個stacklayout也不起作用。

我嘗試過使用xamarin格式的x:FieldModifier="public",即使我使用x:FieldModifier =「public」,「camera」屬性仍然是私有的。這個功能沒用。

據我所知,無法在Xamarin格式的MainActivity.cs中訪問「camera」。

作爲一種解決方法,您可以爲android平臺設計一個頁面呈現器,並在您的頁面呈現代碼中創建一個TextureView

how to create a page render

_textureView =新TextureView(Page1.camera);

順便說一句,初始化TextureView您需要在android平臺上實現ISurfaceTextureListener接口的對象。

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity,ISurfaceTextureListener 
    { 
     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 
      global::Xamarin.Forms.Forms.Init(this, bundle); 
      TextureView textureView = new TextureView(this); 
      textureView.SurfaceTextureListener = this; 
      LoadApplication(new App()); 
     } 
     public void OnSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) 
     { 
      //start 
     } 

     public bool OnSurfaceTextureDestroyed(SurfaceTexture surface) 
     { 
      //stop 
     } 

     public void OnSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) 
     { 
      throw new NotImplementedException(); 
     } 

     public void OnSurfaceTextureUpdated(SurfaceTexture surface) 
     { 
      throw new NotImplementedException(); 
     } 
    } 

請遵循TextureViewguide爲Android。

+0

我沒有想到使用渲染頁面。謝謝你的幫助! –

相關問題