小编Cho*_*890的帖子

Haskell/Yesod中的分组复选框

我目前正在Yesod网络平台上使用Haskell进行开发,我需要在我的一个网页中创建一个表单.此表单必须包含多个复选框,但需要将它们分组到类别中.例如,用户需要在10个复选框,5个项目的列表中选择3个学科,其中最多2个可以是武器.

我在Yesod网站上阅读了本书的部分内容,根据我的理解,我需要创建一个自定义字段来创建分组复选框.不幸的是,我对Haskell并不是很好(刚刚开始使用它1-2周前)而且我很难理解如何实现这一目标.作为参考,这是我在Yesod网站上读到的关于自定义字段的书的页面:http://www.yesodweb.com/book/forms

知道了,我的问题是:如何在Haskell/Yesod中创建分组复选框?

编辑:在我提供的链接中,我理解了Field(fieldView和fieldEnctype)的构造函数的最后两个值,它是第一个(fieldParse),我不知道如何为我的目的进行修改.以下是我引用的链接中的示例:

passwordConfirmField :: Field Handler Text
passwordConfirmField = Field
    { fieldParse = \rawVals _fileVals ->
        case rawVals of
            [a, b]
                | a == b -> return $ Right $ Just a
                | otherwise -> return $ Left "Passwords don't match"
            [] -> return $ Right Nothing
            _ -> return $ Left "You must enter two values"
    , fieldView = \idAttr nameAttr otherAttrs eResult isReq ->
        [whamlet|
            <input id=#{idAttr} name=#{nameAttr} *{otherAttrs} type=password>
            <div>Confirm:
            <input id=#{idAttr}-confirm …
Run Code Online (Sandbox Code Playgroud)

checkbox haskell yesod

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

在MVVM中绑定Validation.HasError属性

我目前正在实现一个ValidationRule检查文本框中是否有一些无效字符.我很高兴设置我已实现的类继承ValidationRule在我的TextBox上,当找到这些字符时将其设置为红色,但我还想使用Validation.HasError属性或Validation.Errors属性弹出一个消息框告诉用户有页面中各种文本框中的错误.

有没有办法将我的属性绑定ViewModelValidation.HasError和/或Validation.Errors属性,以便我可以在我的ViewModel中访问它们?

这是TextBox的错误样式:

<Style x:Key="ErrorValidationTextBox" TargetType="{x:Type pres:OneTextBox}">
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <DockPanel LastChildFill="True">
                    <TextBlock DockPanel.Dock="Right"
                    Foreground="Red"
                    FontSize="12pt"
                    Text="{Binding ElementName=MyAdorner, 
                           Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
                    </TextBlock>
                    <AdornedElementPlaceholder x:Name="MyAdorner"/>
                </DockPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

以下是我在我的XAML中声明我的TextBox(OneTextBox封装常规WPF TextBox)的方法:

<pres:OneTextBox Watermark="Name..." Margin="85,12,0,0" Style="{StaticResource ErrorValidationTextBox}"
                 AcceptsReturn="False" MaxLines="1" Height="22" VerticalAlignment="Top"
                 HorizontalAlignment="Left" Width="300" >
    <pres:OneTextBox.Text>
        <Binding Path="InterfaceSpecification.Name" UpdateSourceTrigger="PropertyChanged">                    
            <Binding.ValidationRules>                       
                <interfaceSpecsModule:NoInvalidCharsRule/>                        
            </Binding.ValidationRules>                    
        </Binding>               
    </pres:OneTextBox.Text>        
</pres:OneTextBox>
Run Code Online (Sandbox Code Playgroud)

c# validation wpf binding mvvm

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

绑定时 DataItem=null,找不到原因?

我试图重现Sheridan 对这个问题的回答中建议的内容,以便在将 WPF 与 MVVM 模式一起使用时浏览我的视图。不幸的是,当我这样做时,我遇到了绑定错误。这是确切的错误:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='JollyFinance.ViewModels.MainViewModel', AncestorLevel='1''. BindingExpression:Path=DataContext.DisplayTest; DataItem=null; target element is 'Button' (Name=''); target property is 'Command' (type 'ICommand')
Run Code Online (Sandbox Code Playgroud)

当我查看 LoginView.xaml 中的 xaml 代码时,我注意到 Visual Studio 告诉我它无法DataContext.DisplayText在 type 上下文中找到MainViewModel。我尝试过删除DataContext.并保留DisplayText,但无济于事。

除非谢里登的答案有错误,否则我肯定在这里遗漏了一些东西。我应该做什么才能让它发挥作用?

主窗口.xaml:

<Window x:Class="JollyFinance.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:JollyFinance.ViewModels"
        xmlns:views="clr-namespace:JollyFinance.Views"
        Title="JollyFinance!" Height="720" Width="1280">

    <Window.Resources>
        <!-- Different pages -->
        <DataTemplate DataType="{x:Type vm:LoginViewModel}">
            <views:LoginView/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type vm:TestViewModel}">
            <views:Test/>
        </DataTemplate> …
Run Code Online (Sandbox Code Playgroud)

c# navigation data-binding wpf mvvm

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

使用ContentControl在WPF中显示视图不起作用

我目前正在研究一个小项目,以便更好地学习WPF,我问自己的一个问题是,如果我想遵循MVVM模式,不同视图之间的导航将如何工作.

我在这里回答了我的问题时偶然发现了这个问题,但不幸的是它似乎没有用.它不是显示我的视图,而是仅显示关联视图模型的命名空间.所以,例如,我没有看到我的LoginView,而是看到一个白色的屏幕上写着"JollyFinance.ViewModels.LoginViewModel".

这是我的MainWindow.xaml代码:

<Window.Resources>
    <!-- Different pages -->
     <DataTemplate x:Key="LoginView" DataType="vm:LoginViewModel">
        <views:LoginView />
    </DataTemplate>
</Window.Resources>

<Window.DataContext>
    <vm:MainViewModel/>
</Window.DataContext>

<Grid>
    <ContentControl Content="{Binding CurrentViewModel}"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)

这是我的MainViewModel.cs:

public class MainViewModel : BindableObject
{
    private ViewModelNavigationBase _currentViewModel;

    public MainViewModel()
    {
        CurrentViewModel = new LoginViewModel();
    }

    public ViewModelNavigationBase CurrentViewModel
    {
        get { return _currentViewModel; }
        set
        {
            if (_currentViewModel != value)
            {
                _currentViewModel = value;
                RaisePropertyChanged();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的LoginViewModel.cs文件:

    public LoginViewModel()
    {
        LoginCommand = new RelayCommand(Login);
        NavigateCommand = new …
Run Code Online (Sandbox Code Playgroud)

c# navigation wpf xaml mvvm

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

使用 time.time() 为函数计时通常返回 0 秒

我必须为我在其中一个类中执行的算法计时,我正在使用 time.time() 函数来执行此操作。实现后,我必须在包含大小数据集的多个数据文件上运行该算法,以便正式分析其复杂性。

不幸的是,在小数据集上,即使我在查看较大数据集的运行时使用该函数获得 0.000000000000000001 的精度,我也会得到 0 秒的运行时间,而且我无法相信它真的比较小的数据集花费的时间少数据集。

我的问题是:使用这个函数是否有问题(如果有,我可以使用另一个具有更好精度的函数)?还是我做错了什么?

如果您需要,这是我的代码:

import sys, time
import random

from utility import parseSystemArguments, printResults

...

def main(ville):
    start = time.time()

    solution = dynamique(ville) # Algorithm implementation

    end = time.time()

    return (end - start, solution)

if __name__ == "__main__":
    sys.argv.insert(1, "-a")
    sys.argv.insert(2, "3")

    (algoNumber, ville, printList) = parseSystemArguments()

    (algoTime, solution) = main(ville)

    printResults(algoTime, solution, printList)
Run Code Online (Sandbox Code Playgroud)

打印结果函数:

def printResults(time, solution, printList=True):
    print ("Temps d'execution = " + str(time) + "s")
    if printList:
        print (solution)
Run Code Online (Sandbox Code Playgroud)

python algorithm time time-complexity

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

由于Alex包装,Yesod平台安装失败

我正在尝试将yesod平台更新到最新版本.为此,我执行了以下命令:

cabal update
cabal install yesod-platform yesod-bin
Run Code Online (Sandbox Code Playgroud)

我最终得到以下错误:

Resolving dependencies...
Configuring language-javascript-0.5.13...
cabal: The program alex version >=3.0.5 is required but the version found at /usr/bin/alex is version 3.0.1
cabal: Error: some packages failed to install:
hjsmin-0.1.4.6 depends on language-javascript-0.5.13 which failed to install
language-javascript-0.5.13 failed during the configure step. The exception was:
ExitFailure 1
yesod-platform-1.2.12.2 depends on language-javascript-0.5.13 which failed to install
yesod-static-1.2.4 depends on language-javascript-0.5.13 which failed to install
Run Code Online (Sandbox Code Playgroud)

经过一番研究后,我偶然发现了以下帖子,其中提到我应该在执行cabal install yesod-platform yesod-bin命令之前手动安装一些依赖项.这些依赖包括alex …

ubuntu haskell cabal cabal-install yesod

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

对数组进行排序,以便所有带有Some的元素都在前面

我有一个F#record type(Request<'a>)定义了一个字段作为('a -> bool) option.在某些时候,我有一个这种记录类型的数组,并希望对它进行排序,以便所有的那些Some ('a -> bool)都是第一个(最低索引),并且所有那些None将是最后一个(最高索引).

我尝试过以下操作,但这似乎不起作用,因为我有一些位于数组的中间/末尾:

let sort (req1:Request<'a>) (req2:Request<'a>) =
    if req1.ResourceCondition.IsSome
    then
        -1
    else if req2.ResourceCondition.IsSome
    then
        1
    else
        0

let reqArray = Array.sortWith sort fifoArray
Run Code Online (Sandbox Code Playgroud)

.net arrays sorting f# optional

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

Python中的冒泡排序没有正确排序

我必须实现冒泡排序作为家庭作业,我的python脚本必须查找2个命令行参数:

-f指定输入文件的文件路径,该文件路径包含每行必须使用冒泡排序排序的数字;

-p,如果指定,则告诉脚本在命令行中打印已排序的数字列表.

此外,我必须在原位实现该算法,这意味着我必须只使用一个列表/数组/ etc而不分配任何其他临时列表/数组/ etc或变量来保存所有数字的一个或一部分以在算法.因此,在我的脚本中,我只使用unsortedList而没有其他任何东西来保存要排序的数字.我从以下链接中获取了冒泡排序算法:冒泡排序作业.

这是我的脚本:

import sys, getopt

def main(argv):
    inputFilePath = ""
    printList = False

    # Traitement pour parser les arguments
    try:
        opts, args = getopt.getopt(argv, "f:p")
    except getopt.GetoptError:
        usage()
        sys.exit()
    for opt, arg in opts:
        if opt in ("-f"):
            inputFilePath = arg
        if opt in ("-p"):
            printList = True

    inputFile = open(inputFilePath, "r")
    unsortedList = [line.rstrip('\n') for line in inputFile]

    sortedList = bubble(unsortedList)
    if printList == True:
        print (sortedList)

def …
Run Code Online (Sandbox Code Playgroud)

python sorting algorithm bubble-sort

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

Android菜单未出现在Google地图活动中(Api v2)

我在android中有一个活动,应该使用谷歌地图api v2显示谷歌地图,顶部有一个菜单.地图看起来应该是这样,但菜单从来没有出现过某种原因.

我已经调试了应用程序,并且onCreateOptionsMenu(Menu menu)从不调用该方法.为什么是这样?

这是我的活动:

public class MapsActivity extends Activity {

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        setUpMapIfNeeded();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_maps, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar …
Run Code Online (Sandbox Code Playgroud)

android google-maps android-menu

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