2013-07-03 17 views
9

Có hướng dẫn nào cho phép tôi thiết kế chế độ xem tùy chỉnh trong xamarin không? Tôi muốn xây dựng chức năng thu phóng pinch cho ứng dụng Android của tôi bằng xamarin.Làm cách nào để xây dựng chế độ xem tùy chỉnh trong xamarin

Tôi đã thử code sau, nhưng nó không làm việc, tôi luôn nhận được android.view.InflateException: Binary XML file line #1: Error inflating class LA_Application.ZoomView lỗi

using System;  
using System.Collections.Generic; 
using System.Linq;  
using System.Text;  
using Android.App;  
using Android.Content;  
using Android.OS;  
using Android.Runtime;  
using Android.Util;  
using Android.Views;  
using Android.Widget;  
using Android.Graphics; 

namespace LA_Application  
{ 

    public class ZoomView : FrameLayout  
    { 

     private ScaleGestureDetector mScaleDetector;  
     private static float mScaleFactor = 1.0f; 


     public ZoomView (Context context) : base (context) 
     { 
      Initialize();  
     } 

     public ZoomView (Context context, IAttributeSet attrs) : base (context,attrs)  
     { 
      Initialize();  
     } 

     public ZoomView (Context context, IAttributeSet attrs, int defStyle) : base (context, attrs, defStyle) 
     { 
      Initialize(); 
     } 

     void Initialize() 
     { 
      mScaleDetector = new ScaleGestureDetector(Context, new ScaleListener()); 
     } 

     public override bool OnTouchEvent (MotionEvent e) 
     { 
      mScaleDetector.OnTouchEvent(e); 

      return true; 
     } 

     protected override void OnDraw(Android.Graphics.Canvas canvas) 
     { 
      base.OnDraw(canvas); 

      canvas.Save();  
      canvas.Scale(mScaleFactor, mScaleFactor); 
      canvas.Restore(); 
     } 
    } 

    private class ScaleListener : ScaleGestureDetector.SimpleOnScaleGestureListener 
    { 
     public override bool OnScale(ScaleGestureDetector detector) 
     { 
      mScaleFactor *= detector.ScaleFactor; 

      // Don't let the object get too small or too large. 
      mScaleFactor = Math.Max(0.1f, Math.Min(mScaleFactor, 5.0f)); 

      return true; 
     } 
    } 
} 

}

và trong file layout

<?xml version="1.0" encoding="utf-8"?> 
<LA_Application.ZoomView xmlns:android="http://schemas.android.com/apk/res/android" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:id="@+id/my_view" /> 

mã ngành

protected override void OnCreate (Bundle bundle) 
{ 
    base.OnCreate (bundle); 

    SetContentView(Resource.Layout.zoomview); 

    /*some code*/ 
} 

Trả lời

1

tôi sẽ đề nghị kiểm tra ra hướng dẫn Java Android này để có được một ý tưởng về những gì bạn cần phải thiết lập:

http://developer.android.com/training/custom-views/index.html

Bạn có thể cần phải tạo ra một thuộc tính tập tin xml cho giao diện tùy chỉnh của bạn.

Một cách tiếp cận bạn có thể muốn xem xét là sử dụng một mảnh thay vì một cái nhìn:

http://docs.xamarin.com/guides/android/platform_features/fragments/fragments_walkthrough

+1

không có tệp xml nào được tạo trong Xamarin, chỉ có tệp C# - bất kỳ ý tưởng nào? –

4

Trong file layout bạn cần phải viết đường dẫn đến lớp học của bạn bằng chữ nhỏ. Đối với tôi Core.Droid.MyImageView phải được viết là core.droid.MyImageView.

Tôi không chắc chắn nếu chỉ có chữ cái đầu tiên hoặc tất cả các chữ cái phải được viết nhỏ, nhưng bạn có thể thử lA_Application.ZoomView hoặc . Một trong số đó sẽ rất có khả năng làm việc :)

+0

Điều này đã khắc phục được sự cố của tôi. Tôi đã sử dụng đúng không gian tên trong dự án của mình và nó đang hoạt động, nhưng nếu bố cục nằm trong một dự án thư viện con, thì tôi cần sử dụng phiên bản chữ thường của không gian tên! –