【问题标题】:How to construct custom views in xamarin如何在 xamarin 中构建自定义视图
【发布时间】:2013-06-30 23:58:33
【问题描述】:

是否有任何教程可以让我在 xamarin 中设计自定义视图?我想使用 xamarin 为我的 android 应用程序构建捏缩放功能。

我尝试了以下代码,但它不起作用,我总是收到android.view.InflateException: Binary XML file line #1: Error inflating class LA_Application.ZoomView 错误

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;
        }  
    }
}

}

在布局文件中

<?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" />

活动代码

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

    SetContentView(Resource.Layout.zoomview);

    /*some code*/
}

【问题讨论】:

    标签: android xamarin android-custom-view


    【解决方案1】:

    我今天遇到了类似的问题,无意中找到了解决方法。只需在定义类 ZoomView 之前添加一个“注册”即可。

    namespace LA_Application    
    {
        [Register("la_application.ZoomView")]
        public class ZoomView : FrameLayout      
        {
            ...
        }
    }
    

    在Visual Studio中右击FrameLayout,然后点击“转到定义”,然后你会在它前面找到一个“注册”。 Visual Studio 告诉我它是 android 运行时属性。我想这才是真正的问题。

    顺便说一句,不要忘记用小写写你的命名空间。希望能对你有所帮助。

    【讨论】:

      【解决方案2】:

      在布局文件中,你需要用小写字母写下你的类的路径。 对我来说,Core.Droid.MyImageView 必须写成 core.droid.MyImageView

      我不确定是否只有第一个字母或所有字母都必须用小写,但您可以尝试lA_Application.ZoomViewla_application.ZoomView。其中一个很可能会起作用:)

      【讨论】:

      • 这解决了我的问题。我在我的项目中使用了正确的命名空间并且它正在工作,但是如果布局在子库项目中,那么我需要使用命名空间的小写版本!
      【解决方案3】:

      这是如何在 xamarin.android 中创建自定义视图的框架

      http://xandroid4net.blogspot.com/2014/09/custom-view.html

      然后将 Emmanuel Touzery 的答案添加到骨架中

      android pinch zoom

      【讨论】:

        【解决方案4】:

        我建议查看此 Java Android 教程以了解您需要设置什么:

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

        您可能需要为您的自定义视图创建一个属性 xml 文件。

        您可能要考虑的另一种方法是使用片段而不是视图:

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

        【讨论】:

        • Xamarin 中没有生成 xml 文件,只有 C# 文件 - 有什么想法吗?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多