小编Dav*_*vid的帖子

MonoDroid:调用自定义视图的构造函数时出错 - TwoDScrollView

我正在构建一个Android应用程序,它使用在此处找到的自定义构建的TwoDScrollView:

http://blog.gorges.us/2010/06/android-two-dimensional-scrollview/

可以在其他几个网站上找到同一个类,Stack Overflow上的其他人也提出了有关它的问题.我在以前使用Java/Eclipse构建的Android应用程序中使用它,并且我取得了成功.

在我目前的应用程序中,我想使用C#和MonoDroid.我决定用C#重写整个TwoDScrollView类.在重写它,然后在一些布局XML中使用它之后,我在尝试运行我的代码时遇到以下异常:

抛出了System.NotSupportedException.无法从本机句柄44f4d310激活MyProject.TwoDScrollView类型的实例.

System.Exception:找不到MyProject.TwoDScrollView ::.ctor(System.IntPtr,Android.Runtime.JniHandleOwnership)......的构造函数......后面有更多文本....

我的布局XML如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<myproject.TwoDScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

</myproject.TwoDScrollView>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

根据以下链接中有关在MonoDroid中使用布局XML中的自定义视图的说明:http://docs.xamarin.com/android/advanced_topics/using_custom_views_in_a_layout

TwoDScrollView类的构造函数如下所示:

public TwoDScrollView(Context context) 
    : base(context)
{
    initTwoDScrollView();
}

public TwoDScrollView(Context context, IAttributeSet attrs) 
    : base(context, attrs)
{
    initTwoDScrollView();
}

public TwoDScrollView(Context context, IAttributeSet attrs, int defStyle) 
    : base(context, attrs, defStyle)
{
    initTwoDScrollView();
}
Run Code Online (Sandbox Code Playgroud)

C#版本中存在与Java版本相同的构造函数(可以在上面的链接中找到).什么可能出错?如果有人想看,我可以发布我的TwoDScrollView的完整C#代码.除了用C#重写之外,它基本上与位的Java代码位相同.

谢谢你的帮助!

android scrollview android-custom-view android-layout xamarin.android

45
推荐指数
3
解决办法
3万
查看次数

从C#中的基类调用重写的方法

给出以下C#类定义和代码:


public class BaseClass
{
    public virtual void MyMethod()
    {
        ...do something...
    }
}

public class A : BaseClass
{
    public override void MyMethod()
    {
        ...do something different...
    }
}

public class B : BaseClass
{
    public override void MyMethod()
    {
        ...do something different...
    }
}

public class AnotherObject
{
    public AnotherObject(BaseClass someObject)
    {
        someObject.MyMethod(); //This calls the BaseClass method, unfortunately.
    }
}
Run Code Online (Sandbox Code Playgroud)

我想调用实际在A或B中找到的MyMethod(),假设传入的对象实际上是A或B的实例,而不是在BaseClass中找到的实例.做这样的事情:


public class AnotherObject
{
    public AnotherObject(BaseClass someObject)
    {
        A temp1 = someObject as A;
        if (A …
Run Code Online (Sandbox Code Playgroud)

c# methods virtual inheritance overriding

19
推荐指数
2
解决办法
2万
查看次数

使用XAML中的系统类型作为资源

我遇到过一种情况,即直接在XAML中指定浮点值并将其用作我的几个UI部件的资源非常有用.在搜索之后,我发现了大量有关如何在XAML中包含正确的程序集(mscorlib)的信息,因此您可以做到这一点.

不幸的是,我在一个实例中遇到异常,我尝试这样做.以下是重新创建情况的以下XAML:

<Window x:Class="davidtestapp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:core="clr-namespace:System;assembly=mscorlib"
    Title="MainWindow" Height="350" Width="525">

<Window.Resources>
    <core:Double x:Key="MyDouble">120</core:Double>
</Window.Resources>

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="{StaticResource MyDouble}" />
        <ColumnDefinition Width="40" />
        <ColumnDefinition Width="40" />
    </Grid.ColumnDefinitions>

    <Rectangle Grid.Column="0" Fill="Red" />
    <Rectangle Grid.Column="1" Fill="Green" />
    <Rectangle Grid.Column="2" Fill="Blue" />

</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

当我尝试编译并运行它时,我得到一个XamlParseException,它向我说"'120'不是属性'Width'的有效值".

但是"Width"属性双重的,为什么我不能使用定义的StaticResource来设置它?有谁知道如何做到这一点?

wpf xaml resourcedictionary mscorlib staticresource

19
推荐指数
1
解决办法
2万
查看次数

如何绑定到WPF ColorAnimation中的颜色?

我想做一些看似很简单的事情,但我无法弄明白该怎么做.我有一个在MouseEnter事件发生时触发的ColorAnimation.它只是将边框的背景颜色从一种颜色更改为另一种颜色.

不幸的是,我无法弄清楚如何将任何硬编码颜色放入此ColorAnimation中.所以它看起来像这样:

<Style x:Key="MouseOverStyle">
<Style.Triggers>
    <EventTrigger RoutedEvent="Mouse.MouseEnter">
        <EventTrigger.Actions>
            <BeginStoryboard>
                <Storyboard>
                    <ColorAnimation Duration="0:0:0.5" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
                                    To="Red" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger.Actions>
    </EventTrigger>
</Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)

但是,我想做一些像这样的事情:

<SolidColorBrush x:Key="MyEventColor" Color="{Binding EventColor}" />

<Style x:Key="MouseOverStyle">
<Style.Triggers>
    <EventTrigger RoutedEvent="Mouse.MouseEnter">
        <EventTrigger.Actions>
            <BeginStoryboard>
                <Storyboard>
                    <ColorAnimation Duration="0:0:0.5" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
                                    To="{StaticResource MyEventColor}" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger.Actions>
    </EventTrigger>
</Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)

或者像这样:

<Style x:Key="MouseOverStyle">
<Style.Triggers>
    <EventTrigger RoutedEvent="Mouse.MouseEnter">
        <EventTrigger.Actions>
            <BeginStoryboard>
                <Storyboard>
                    <ColorAnimation Duration="0:0:0.5" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
                                    To="{Binding EventColor}" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger.Actions>
    </EventTrigger>
</Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)

当我尝试执行其中任何一个时,会抛出异常.对于第一个,它抛出一个异常,告诉我基本上"Color"属性不能采用SolidColorBrush值...这是有道理的...但它肯定没有帮助我,因为ColorAnimation不会让我动画"(Border.Background).(SolidColorBrush)"属性...它只会让我为"(Border.Background).(SolidColorBrush.Color)"属性设置动画....

第二个例子的例外基本上告诉我它"无法冻结这个Storyboard时间轴树以供跨线程使用"...所以听起来像ColorAnimation试图在除UI线程之外的其他一些线程中做这个绑定?无论它想做什么......它都无法正常工作.

我怎么能做这么简单的任务呢?

data-binding wpf resources colors coloranimation

5
推荐指数
1
解决办法
4149
查看次数

Visual Studio 安装程序项目包含太多 DLL?

我正在尝试使用 Visual Studio 安装程序项目为我的应用程序制作安装程序。我使用的是 Visual Studio 2015,我的应用程序是使用 WPF 和 .NET 4.5 构建的。

我正在遵循有关如何制作 Visual Studio 安装程序项目的一般教程:https://www.youtube.com/watch?v=z0v6hmumCFU

当我在安装程序中包含每个项目的“主要输出”时,它会识别这些项目的依赖项,并将它们作为需要包含在安装程序中的文件包含在内。然而,我很困惑,因为它甚至似乎包含几个标准的“系统”.NET DLL 文件,如下图所示:

Visual Studio 安装程序项目的图像

我尝试构建它,然后运行它构建的安装程序,果然它会将所有这些“系统”DLL 文件安装到安装目录中。

我的理解是,一旦用户安装了 .NET 4.5 或更高版本,这些 DLL 文件就应该位于用户计算机上的标准位置,因此我的安装程序不需要将许多这些依赖项复制到安装目录中。

所以我的问题是:

(1) 为什么 Visual Studio 安装程序项目扩展包含这些显然是 .NET 一部分的依赖项,并且实际上应该位于标准 .NET 安装位置?

(2) 排除它们可以吗?

(3)有没有办法让VS安装程序项目扩展根本不添加它们?

.net c# installation visual-studio visual-studio-2015

5
推荐指数
0
解决办法
1082
查看次数

如何不将app.config文件复制到输出目录

我正在构建一个WPF应用程序.我正在使用Visual Studio社区2015.为了创建"真正的"发布版本,我正在更改一些构建设置,因此它只生成将安装在用户计算机上的必要文件.所以没有vshost exe,没有生成的XML文件等等(但我保留了PDB文件生成).

除了一个文件,我拥有我想要的一切.我在每个项目中都有一个App.config文件.我不介意将其复制到我的调试版本的输出目录中,但在我的"真正"版本构建中,我不希望它存在.

该文件已在其属性中设置为"不复制".见下图:

App.config图片

尽管有"不要复制"设置,但我仍然会在每次构建项目时生成"MyAppName.exe.config"文件.为什么会这样?我怎么能把它关掉?这很烦人.

谢谢你的建议!

c# wpf app-config visual-studio-project visual-studio-2015

5
推荐指数
1
解决办法
5602
查看次数

Android:使用我工作区中其他项目的资源

我是Android开发的新手 - 来自WPF背景.

我在Eclipse的"工作区"中创建了两个Android项目.一个称为"TestProject",另一个称为"TestLibraryProject".

在"TestLibraryProject"内部,我创建了一个名为BlueBox的自定义视图.

我现在想在我的主要布局"TestProject"中使用BlueBox.知道我需要以某种方式从"TestProject"引用"TestLibraryProject",我右键单击"TestProject"并按照菜单操作:

构建路径 - >添加库

在弹出的对话框中,我选择了"Android Classpath Container",在后面的对话框中,我从组合框中选择了"TestLibraryProject".

双重问题:1.这是将"TestLibraryProject"包含在"TestProject"中以便我可以使用的正确方法吗?2.我现在如何在主布局XML中使用BlueBox?

我尝试将其放入XML文件中:

<mypackage.TestLibraryProject.BlueBox
        android:id="@+id/my_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
Run Code Online (Sandbox Code Playgroud)

但那没用.我还尝试在我的根布局元素中引用命名空间:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:binding="http://www.gueei.com/android-binding/"
    xmlns:testlibrary="http://schemas.android.com/apk/res/android/mypackage2.TestLibraryProject"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
Run Code Online (Sandbox Code Playgroud)

但这似乎没有帮助.

有什么建议?想法?救命?

谢谢!

java xml resources android multiple-projects

4
推荐指数
1
解决办法
2731
查看次数

.Net MAUI 中单选按钮样式的问题

我在 .Net MAUI 中设置单选按钮样式时遇到了一些问题。最初,我注意到 Windows 和 Android 上的单选按钮外观并不一致,如下图所示:

单选按钮的外观

它们不仅不同,而且按钮样式的可用选项也很有限。定义颜色的唯一选项是“BorderColor”、“BackgroundColor”和“TextColor”。“BorderColor”和“BackgroundColor”对单选按钮本身的颜色完全没有影响。我想更改实际单选按钮的颜色。

所以我决定创建一个控制模板来帮助我。这是我的控件模板:

<ControlTemplate x:Key="RadioButtonTemplate">
    <Border Stroke="Transparent" BackgroundColor="Transparent">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroupList>
                <VisualStateGroup x:Name="CheckedStates">
                    <VisualState x:Name="Checked">
                        <VisualState.Setters>
                            <Setter TargetName="check" Property="Opacity" Value="1" />
                        </VisualState.Setters>
                    </VisualState>
                    <VisualState x:Name="Unchecked">
                        <VisualState.Setters>
                            <Setter TargetName="check" Property="Opacity" Value="0" />
                        </VisualState.Setters>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateGroupList>
        </VisualStateManager.VisualStateGroups>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="20" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <Grid WidthRequest="20" HeightRequest="20" Grid.Column="0" VerticalOptions="Center" HorizontalOptions="Center">
                <Ellipse x:Name="border_circle" StrokeThickness="2" Stroke="DarkBlue" Fill="White" WidthRequest="18" HeightRequest="18" HorizontalOptions="Center" VerticalOptions="Center" />
                <Ellipse x:Name="check" Fill="DarkBlue" WidthRequest="10" HeightRequest="10" HorizontalOptions="Center" VerticalOptions="Center" />
            </Grid>
            <ContentPresenter Margin="10,0,0,0" Grid.Column="1" HorizontalOptions="Start" …
Run Code Online (Sandbox Code Playgroud)

xaml controltemplate radio-button maui

3
推荐指数
1
解决办法
3094
查看次数

无法在WPF中绑定ContentPresenter的可见性

我有以下XAML:

<UserControl.Resources>

<DataTemplate x:Key="ExpanderTemplate">
    <Grid>
        <Rectangle Stroke="Black" StrokeThickness="1" Width="10" Height="10" Fill="White" />
        <Rectangle Stroke="Black" StrokeThickness="1" Width="6" Height="1" Fill="Black" />
        <Rectangle Stroke="Black" StrokeThickness="3" Width="2" Height="6" Fill="Black" />
    </Grid>
</DataTemplate>

<DataTemplate x:Key="CollapserTemplate">
    <Grid>
        <Rectangle Stroke="Black" StrokeThickness="1" Width="10" Height="10" Fill="White" />
        <Rectangle Stroke="Black" StrokeThickness="1" Width="6" Height="1" Fill="Black" />
    </Grid>
</DataTemplate>

</UserControl.Resources>

<Grid>
<StackPanel Orientation="Horizontal">
    <TextBlock Text="{Binding Path=Name}" />
    <Grid>
        <ContentPresenter x:Name="ExpanderPresenter" ContentTemplate="{StaticResource ExpanderTemplate}" Visibility="{Binding ExpanderVisibility}" />
        <ContentPresenter x:Name="CollapserPresenter" ContentTemplate="{StaticResource CollapserTemplate}" Visibility="{Binding CollapserVisibility}" />
    </Grid>
</StackPanel>
</Grid>
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,它本质上是一个文本块,其内容绑定到一个名称,以及两个内容提供者,它们的可见性绑定到源类上的几个Visibility对象.源类看起来像这样:

public class MyViewModel
{
public string …
Run Code Online (Sandbox Code Playgroud)

data-binding wpf binding visibility contentpresenter

1
推荐指数
1
解决办法
2437
查看次数

Android:在自定义视图组中使用onTouchEvent和自定义视图

我有一个自定义视图,我称之为"节点",它是一个名为"NodeGrid"的自定义ViewGroup的子节点."NodeGrid"类更具体地扩展了RelativeLayout.

我在自定义视图类("Node")中有以下代码片段:

private boolean isBeingDragged = false;

@Override
public boolean onTouchEvent(MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN)
    {
        isBeingDragged = true;
    }
    else if (event.getAction() == MotionEvent.ACTION_UP)
    {
        isBeingDragged = false;
    }
    else if (event.getAction() == MotionEvent.ACTION_MOVE)
    {
        if (isBeingDragged)
        {
            float xPosition = event.getX();
            float yPosition = event.getY();

            //change the x and y position here
        }
    }

    return false;
}
Run Code Online (Sandbox Code Playgroud)

问题:

在这段代码中有设置断点后,它似乎像的onTouchEvent获取调用只为MotionEvent.ACTION_DOWN情况下,而不是任何的另外两起案件("动作"或"动作移动").有没有人知道可能导致这种情况发生的任何事情?

另外(可能是相关的):

不要紧,如何视图被添加到一个ViewGroup?我注意到除了"addView"之外,还有其他方法可以将子项添加到ViewGroup,例如"addFocusables"和"addTouchables".现在我只是使用"addView"将子视图添加到ViewGroup.

android drag-and-drop view touch-event viewgroup

1
推荐指数
1
解决办法
9498
查看次数