小编Rob*_*met的帖子

如何轻松处理方向变化

我正在开发一个Android应用程序,它可以添加每个视图程序.当用户转动屏幕时,我只想要填写的值再次显示.
是否有一种简单的方法让Android自动执行此操作?

我的应用程序是完全动态的,因此它没有预先确定的布局,这使得它变得更加困难.

那么,我怎样才能轻松保存屏幕状态?

android orientation-changes

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

从静态方法调用startActivityForResult

我有一个buttonlistener,当用户点击按钮时,我想启动一个相机意图.目前我有这个:

public class ButtonListener implements View.OnClickListener 
{
private ArrayList<String> connectedItems;
private String identifier = null;
private Context context;
private EnteredValues enteredValues;

public ButtonListener(Context c, String identifier, ArrayList<String> connectedItems) {
    this.connectedItems = connectedItems;
    this.identifier = identifier;
    this.context = c;
}

public void onClick(View v) {
    if (identifier.equals(ButtonItem.takePhoto)) {
        MainActivity.takePhoto();
    }

}
Run Code Online (Sandbox Code Playgroud)

现在我想在mainActivity中调用一个方法,然后我想启动startActivityForResult,但是我得到一个错误,我无法从静态方法调用startActivityForResult.

public class mainActivity extends Activity{
...
   public static void takePhoto(){
       startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE));
          break;

       //Here I get the error..
   }
Run Code Online (Sandbox Code Playgroud)

}

解决此类问题的最佳做法是什么?将mainActivity对象传递给我的buttonListener还是有其他选项?

很多thx :)

android

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

显示一种帮助“覆盖”

我想让我的应用程序对用户更加友好,所以我想在用户第一次启动应用程序时显示一种突出显示不同组件的叠加层。

开始实施的最佳方法是什么?

下面是一个例子:

在此处输入图片说明

android

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

接受多个枚举类型的方法

我有一个3类枚举类

我想有一个方法,可以将所有3个枚举作为参数,并获取枚举的整数值.

public enum Enum1
{
    Fire = 0,
    Hour_24 = 1,
    Key_Switch = 2,
    Follower = 3,
    Entry_Delay1 = 4,
    Entry_Delay2 = 5,
    Intertior = 6,
    Local_Only = 7,
}

public enum Enum2
{
    Faulted = 0,
    Tampered = 1,
    Trouble = 2,
    Bypassed = 3,
    Inhibited = 4, 
    Low_Battery = 5,
    Loss_Supervision = 6,
    Reserved,
    Alarm_Memory = 8,
    Bypass_Memory = 9
}

private void BuildMessage ()
{
     List<Enum1> Enum1List = new List<Enum1>();
     FillBits(Enum1List);  // => Here I get …
Run Code Online (Sandbox Code Playgroud)

c# enums

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

只有最后一个usercontrol才会显示contentcontrol

我有一个奇怪的问题.我创建了一个带有标签和画布的usercontrol.画布引用资源.

但画布仅显示在我的stackpanel中的最后一个控件上.

这是我的窗口

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:UserControlSolution" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="UserControlSolution.MainWindow"
    Title="MainWindow" Height="836" Width="270.5" Background="#FF2B2B2B" BorderBrush="{DynamicResource Border}" Loaded="Window_Loaded" >

<Window.Resources>

    <LinearGradientBrush x:Key="Border" EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
        <GradientStop Color="#FF6C6C6C" Offset="0.009"/>
        <GradientStop Color="#FFA1A1A1" Offset="1"/>
    </LinearGradientBrush>
    <SolidColorBrush x:Key="Orange" Color="#FFF5610E"/>
    <SolidColorBrush x:Key="Red" Color="#FFE51400"/>
    <SolidColorBrush x:Key="Blue" Color="#FF1BA1E2"/>
    <SolidColorBrush x:Key="Yellow" Color="#FFFFC40D"/>

</Window.Resources>

<StackPanel Margin="0,10,0,0">
    <local:UserControlButton x:Name="Robby" Height="50" VerticalAlignment="Top" Margin="2,0,0,5"/>
    <local:UserControlButton x:Name="Erwin" Height="50" VerticalAlignment="Top" Margin="2,0,0,5" />
    <local:UserControlButton x:Name="Laurens" Height="50" VerticalAlignment="Top"  Margin="2,0,0,5"/>
    <local:UserControlButton x:Name="Kevin" Height="50" VerticalAlignment="Top" Margin="2,0,0,5"/>
    <local:UserControlButton x:Name="Liesbeth" Height="50" VerticalAlignment="Top" Margin="2,0,0,5" Background="{DynamicResource Orange}"/>
    <local:UserControlButton x:Name="Jack" Height="50" VerticalAlignment="Top" Margin="2,0,0,5"/> …
Run Code Online (Sandbox Code Playgroud)

c# wpf user-controls canvas

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

将弹出窗口保持在窗口范围内

我正在尝试在按钮下显示一个弹出窗口。

这是现在的样子:

在此处输入图片说明 但我希望它留在窗口边界内,像这样(油漆中的例子)

在此处输入图片说明

这是我的顶部栏,底部声明了弹出窗口:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="55" />
        <RowDefinition Height="35" />
    </Grid.RowDefinitions>

    <Grid x:Name="AdminContainer" Grid.Row="0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="4*" />
            <ColumnDefinition Width="10" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>

        <Border Background="Transparent">
            <Viewbox StretchDirection="DownOnly" Stretch="Uniform" Height="35" HorizontalAlignment="Left" Margin="10,0,0,0">
                <ContentControl Content="{StaticResource LynxLogo}" Margin="0,5" />
            </Viewbox>
        </Border>

        <ToggleButton x:Name="AdminOptionsToggleButton" 
                      Grid.Column="2" 
                      VerticalAlignment="Center"
                      HorizontalAlignment="Stretch"
                      IsChecked="{Binding mainViewModel.OptionsPopupOpen, Mode=TwoWay}"
                      Style="{StaticResource EmptyToggleButtonStyle}"
                      Cursor="Hand">

            <Grid x:Name="AdminPanel" HorizontalAlignment="Right" Height="45"
                  VerticalAlignment="Stretch" Margin="0,0,2,0" >
                <Grid.Style>
                    <Style>
                        <Setter Property="Grid.Background" Value="Transparent"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=ToggleButton}, Path=IsMouseOver}" Value="True">
                                <Setter Property="Grid.Background" Value="#FF404040"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=ToggleButton}, …
Run Code Online (Sandbox Code Playgroud)

c# wpf popup

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

将textcolor按钮设置回默认值

我有一个登录屏幕.在这个登录屏幕中,我有一个默认禁用的按钮.

当用户输入4个数字时,我启用该按钮并将文本颜色更改为绿色.
但是当4个数字不是正确的代码时,我清除了我的edittext并再次禁用了我的按钮.

目前,此禁用按钮的文本颜色为偏离绿色.如何将其重新设置为默认颜色?

public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(s.length() >= maxLength)
            {
                btnOk.setEnabled(true);
                btnOk.setTextColor(Color.parseColor("#00B32D"));
            }

            else
            {
                btnOk.setEnabled(false);
            }


private void checkIfValid(String inputPin)
{
    if(inputPin.equals("0000"))
    {
        startActivity(new Intent(this, ShowScreenActivity.class));
        finish();
    }
    else
    {
        clearText();

      ====>   //Here i want to set my textcolor back to normal.  

        Toast.makeText(this, "Pincode foutief", Toast.LENGTH_SHORT).show();
    }
}
Run Code Online (Sandbox Code Playgroud)

android

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

在列表框中更改所选用户控件的背景颜色

我有一个包含一些项目的列表框.选择项目时,我想更改UserControlButton的背景颜色.

我怎样才能做到这一点?

<Window.Resources>
    <Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    </Style>
</Window.Resources>

<Border x:Name="UserScrollContainer">
       <ListBox x:Name="UserContainer" ItemsSource="{Binding allUserViewModel.Users}" 
                             Background="Transparent"   
                             HorizontalContentAlignment="Stretch"
                             ScrollViewer.HorizontalScrollBarVisibility="Hidden" 
                             ScrollViewer.VerticalScrollBarVisibility="Visible"
                             BorderThickness="0" Margin="0" Padding="0"    
                             ItemContainerStyle="{DynamicResource ListBoxItemStyle}">

            <ListBox.ItemTemplate>
                 <DataTemplate>
                      <local:UserControlButton x:Name="UserControlButton" />   
                                // Change background color depending if it is selected
                  </DataTemplate>
            </ListBox.ItemTemplate>
      </ListBox>
</Border>     
Run Code Online (Sandbox Code Playgroud)

编辑

我知道我可以添加这样的东西:

        <Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>

        <Setter Property="Background" Value="Lightblue"/>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="true">
                <Setter Property="Background" Value="Red"/>
            </Trigger>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter Property="Background" Value="Yellow"/>
            </Trigger>
        </Style.Triggers>
    </Style>
Run Code Online (Sandbox Code Playgroud)

但后来我得到了这个结果:

在此输入图像描述

我需要更改usercontrol的背景,而不是listboxitem的背景.

wpf listbox

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

无法将MS.Internal.NamedObject类型的对象转换为System.Windows.Datatemplate

我想根据枚举更改图标.

我为UserControl创建了一个名为CallControlViewModel的新视图模型

public class CallControlViewModel : BaseViewModel
{
    private InputTypeEnum _inputTypeEnum;

    public CallControlViewModel()
    {

    }

    public InputTypeEnum InputType
    {
        get { return _inputTypeEnum; }
        set
        {
            if (_inputTypeEnum != value)
            {
                _inputTypeEnum = value;
                NotifyPropertyChanged("InputType");
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是baseViewModel

public class BaseViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Notify of Property Changed event
    /// </summary>
    /// <param name="propertyName"></param>
    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }        
}
Run Code Online (Sandbox Code Playgroud)

这是枚举 …

c# wpf

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

使用转换器更改usercontrol的backgroundcolor

我有一个usercontrol,我想动态地更改此控件的背景颜色.

当视图模型一定枚举设置我想要的颜色改变,所以我用一个转换器将枚举转换成的SolidColorBrush.

当我将此转换器放在另一个控件中时,它工作正常.但是当我将它放在xaml文件顶部的usercontrol属性中时,它会出错.

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:ee="http://schemas.microsoft.com/expression/2010/effects"
mc:Ignorable="d"
xmlns:converters="clr-namespace:UserControlSolution.Converter"
x:Class="UserControlSolution.UserControlButton"
x:Name="UserControl"
Height="50" 
VerticalAlignment="Top"
Background="{Binding CallStatus, Converter={StaticResource CallStatusBackgroundConverter}}"
Margin="2,0,0,5"
>

<UserControl.Resources>
    <converters:CallStatusEnumToBackgroundColor x:Key="CallStatusBackgroundConverter"/>
    <converters:StatusEnumToStatusResourceConverter x:Key="StatusIconConverter"/>
    <BooleanToVisibilityConverter x:Key="BoolToVis" />

    <Style x:Key="SelectedStyle" TargetType="{x:Type WrapPanel}">
        <Setter Property="Background" Value="Transparent"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="true">
                <Setter Property="Background" Value="{Binding CallStatus, Converter={StaticResource CallStatusBackgroundConverter}}"/>
                <Setter Property="Opacity" Value=".92"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>

<Grid x:Name="LayoutRoot" VerticalAlignment="Top" HorizontalAlignment="Stretch">        
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="30"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="50"/>
    </Grid.ColumnDefinitions>
 </Grid>
Run Code Online (Sandbox Code Playgroud)

我的转换器只是从枚举转换为颜色

namespace UserControlSolution.Converter
{
    [ValueConversion(typeof(CallEnum), typeof(SolidColorBrush))]
    public class CallStatusEnumToBackgroundColor …
Run Code Online (Sandbox Code Playgroud)

c# wpf user-controls

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

标签 统计

c# ×5

wpf ×5

android ×4

user-controls ×2

canvas ×1

enums ×1

listbox ×1

orientation-changes ×1

popup ×1